picture picture
October 25, 2011 PHP 0 Comments

Browser Detection script in PHP

Browser Detection script in PHP

PHP code to detect browser name/version.

[code type=php]
$useragent = $_SERVER [‘HTTP_USER_AGENT’];
echo “Your User Agent is: ” . $useragent;
?>
[/code]

October 25, 2011 PHP 0 Comments

HTTP Redirection in PHP

PHP code to redirect any HTTP url.

[code type=php]
<?php
header(‘Location: http://you_domain.com/url.php’); // stick your url here
?>
[/code]

October 25, 2011 PHP 0 Comments

Seconds to String

This function will return the duration of the given time period in days, hours, minutes and seconds.

e.g. secondsToStr(1234567) would return “14 days, 6 hours, 56 minutes, 7 seconds”
[code type=php]
<?php
function secondsToStr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.’ day’;if($days<>1){$r.=’s’;}if($secs>0){$r.=’, ‘;}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.’ hour’;if($hours<>1){$r.=’s’;}if($secs>0){$r.=’, ‘;}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.’ minute’;if($minutes<>1){$r.=’s’;}if($secs>0){$r.=’, ‘;}}
$r.=$secs.’ second’;if($secs<>1){$r.=’s’;}
return $r;
}

?>
[/code]

October 25, 2011 PHP 1 Comments

Get Remote IP Address in PHP

PHP Script to find out the IP address.

[code type=php]

<?php
function getRemoteIPAddress() {
$ip = $_SERVER[‘REMOTE_ADDR’];
return $ip;
}
?>
[/code]

The above code will not work in case your client is behind proxy server. In that case use below function to get real IP address of client.

[code type=php]
<?php
function getRealIPAddr()
{
if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) //check ip from share internet
{
$ip=$_SERVER[‘HTTP_CLIENT_IP’];
}
elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) //to check ip is pass from proxy
{
$ip=$_SERVER[‘HTTP_X_FORWARDED_FOR’];
}
else
{
$ip=$_SERVER[‘REMOTE_ADDR’];
}
return $ip;
}
?>
[/code]

November 23, 2009 PHP 0 Comments

Find start and end date of week in PHP

I need to check if the current day is a Monday and then figure out the dates of last week’s Monday and Sunday. Here is my solution which is simpler:

[code type=php]
if(date(‘N’) == 1) {
$prior_week = date(‘W’) – 1;
if($prior_week == 0) {
$prior_week = 52;
$year = date(‘Y’) – 1;
}else
$year = date(‘Y’);

echo date(“Y-m-d”, strtotime($year.’W’.$prior_week.’1′));
echo date(“Y-m-d”, strtotime($year.’W’.$prior_week.’7′));
}

?>
[/code]

So if we have a Monday which we have when date(‘N’) is 1, we get the current week number of the year with date(‘W’). Last week’s number should then be that number minus one. Note that we need to take extra precautions to to switch the year to the prior year if we have to.

Finally we use the old date and strtotime combo to get the dates in any format we want, in this case as YYYY-MM-DD. The format used in the strtotime call is an ISO8601 format that incorporates the day of week number and the week of year number. Without strtotime’s ability to understand this format the above solution would not work.