PHP Research And Development

Sharing PHP Script, PHP News, Download web application, web tutorial, Learning PHP and more

What PHP Script?  

Definition: Server side scripting means that all of the code is executed on the server before the data is passed to the user's browser. In the case of PHP this means that no PHP code ever reaches the user, it is instead executed and only the information it outputs is sent to the web browser.

One way to see this in action is to open one of your PHP pages in a web browser and then choose the 'View Source' option. You will not see any PHP code; you will only see what the PHP has outputted because all of the code was executed on the server before it was delivered to the browser.

Examples:
$Cat = "Spot";
$Dog = "Clif";
Print "My cat " . $Cat . " and my dog " . $Dog . " like to play together.";
?>

While the PHP file may contain all of the information above, your browser will only ever receive the information: My cat Spot and my dog Clif like to play together.

Read More...
AddThis Social Bookmark Button

 

PHP Visitor Tracking with Cookies

Cookies allow the webmaster to store information about the site visitor on their computer to be accessed again the next time they visit. One common use of cookies is to store your username and password on your computer so you don't need to login again each time you visit a website. Cookies can also store other things such as your name, last visit, shopping cart contents, etc.

The main difference between a cookie and a session is that a cookie is stored on your computer, and a session is not. Although cookies have been around for many years and most people do have them enabled, there are some who do not. Cookies can also be removed by the user at any time, so don't use them to store anything too important.

A cookie is set with the following code: setcookie(name, value, expiration)

$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>


The above code sets a cookie in the visitor's browser called "AboutVisit".

The cookie sets the value to the current date, and set's the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.) Now let's retrieve the cookie


if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
echo "Welcome back!
You last visited on ". $last;
}
else
{
echo "Welcome to our site!";
}
?>



This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.

TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!

To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. This is often done when you 'logout' of a site. Here is an example:

$past = time() - 10;
//this makes the time 10 seconds ago
setcookie(AboutVisit, date("F jS - g:i a"), $past);
?>
REMEMBER:Cookies need to be set in the header. This means they must be sent before any HTML is set to the page, or they will not work.

Read More...
AddThis Social Bookmark Button