picture picture
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.

November 22, 2006 PHP 0 Comments

Using cURL in PHP

cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:

  • Implement payment gateways payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.
A typical PHP cURL usage follows the following sequence of steps.

curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_exec – Executes a cURL session.

curl_close – Closes the current cURL session.

Below are some examples which should make the working of cURL more clearer.

Download file or web page using PHP cURL

The below piece of PHP code uses cURL to download Google’s RSS feed.

[code type=php]
/**
* Initialize the cURL session
*/
$ch = curl_init();

/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, ‘http://news.google.com/news?hl=en&topic=t&output=rss’);

/**
* Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);

/**
* Close cURL session
*/
curl_close ($ch);
[/code]

As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

The above code uses two such options.

CURLOPT_URL: Use it to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the contents instead of echoing them to the browser.

Download file or web page using PHP cURL and save it to file

The below PHP code is a slight variation of the above code. It not only downloads the contents of the specified URL but also saves it to a file.

[code type=php]
/**
* Initialize the cURL session
*/
$ch = curl_init();

/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL, ‘http://news.google.com/news?hl=en&topic=t&output=rss’);

/**
* Create a new file
*/
$fp = fopen(‘rss.xml’, ‘w’);

/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);

/**
* Execute the cURL session
*/
curl_exec ($ch);

/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
[/code]
Here we have used another of the cURL options, CURLOPT_FILE. Obtain a file handler by creating a new file or opening an existing one and then pass this file handler to the curl_set_opt function.

cURL will now write the contents to a file as it downloads a web page or file.