|
• Tutorials: PHP: Creating a cookie PHP is short for Post Hypertext Peripheral Coding. It's a great programming language that lets you do many things never thought possible. You can change your layout without having to edit every single page of your site. It's not complicated to learn if you're familiar to SSI and it's very handy. Before you convert anything to PHP make sure your host supports PHP or else it won't work. Free web hosts such as: FreeWebsites.com, Geocities, Angelfire, etc. do not support PHP! The code below creates a cookie on the visitor's browser. <?php $value = "something here"; setcookie("cookiename",$value,time()+3600,"/",".domain.com"); // PHP 3 echo $cookiename; // PHP 4.1.0 echo $_COOKIE["cookiename"]; ?> 1st step: Change "something here" to any value you want stored, change "cookiename" to the name you want to call the cookie, and finally change domain.com to your own domain. 2nd step: The "time()+3600" portion of the code tells it when to expire, on the coding above I set it to expire one hour from now(3600 seconds = 1 hour). You can change that value depeding on when you want it to expire(example: For 24 hours you would have to change it to "time()+(3600*24)" ). 3rd step(optional): To delete a cookie that you have created you will have to paste the same coding above but you need to set the expiration time to "time() - 3600", meaning 1 hour ago. NOTE: Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. Cookies must be deleted with the same parameters as they were set with. |