iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning PHP Course: Cookies - www.itechcollege.com PHP Course: Cookies - www.itechcollege.com
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

courses > PHP > Cookies

Cookies

To create and modify a cookie, use the PHP function setcookie():

setcookie('uid', 'John');

Then, you can access the value "John" of the variable uid in every further page on your site viewed by this browser. This type of cookie is called "session cookie", because it lasts for the length of a user's session.

If you want to keep the cookie after the person exits his or her browser, you must pass setcookie() through a third parameter, the date you want the cookie to expire:

<?php
$uName = 'John';
$exitDate = mktime(0,0,0,1,1,2002);
setcookie('uid', $uName, $exitDate);
?>

So the cookie will end with January 1, 2002.

If you want to update a cookie to store a newer value, you can simply overwrite its value:

<?php
$uName = 'Mike';
$exitDate = mktime(0,0,0,1,1,2002);
setcookie('uid', $uName, $exitDate);
?>

Here is the syntax for setcookie() with five arguments:

<?php
setcookie(name, value, expire, path, domain);
?>

For example:

<?php
setcookie('uid', 'John', $exitDate, '~/home', '.abc.com');
?>

It is very simple to delete a cookie: simply left the value of varible as null and just keep the name:

<?php setcookie('uid'); ?>

Note: any cookie must be set before you print any other contents:

<?php
setcookie('uid', 'John');
echo "Hello world";
?>

To get cookie value, use:

<?php
echo $_COOKIE['uid'];
?>

If you set cookie value 'John' for 'uid', then it will print 'John' on the screen.



  Back to Index Page
 Next Page:  

Basic Tags
Statements Comments and Characters
echo
Variables
Array
Loop
The If Statement
Function
Send and get Query String
Date and Time
Cookies
Environment Variables
Use Database
Create Graphics


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech