setcookie() function must be used at the top of your PHP script. No white spaces or any kind of text should precede the calling of the function. In the example below, we use a form to collect the name and age of a user and then store that information in a cookie:<?php
if(isset($_POST['submit'])){
$err = "";
echo "dghsdgh";
//check if the form values are not empty
if(empty($_POST['txtname'])){
$err = "Please enter a name.";
}
if(empty($_POST['txtage'])){
$err .= "Please enter a age.";
}
if(strlen($err) < 1 ){
setcookie('User', $_POST['txtname']);
setcookie('Age',$_POST['txtage']);
echo "Cookies set";
}else{
echo "The following errors occurred: ".$err;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Handling Cookies in PHP</title>
<style type="text/css">
<!--
.style1 {font-size: 14px}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="setcookie.php">
<table width="100%" border="1">
<tr>
<td colspan="2"><h1>Set Cookie Form </h1></td>
</tr>
<tr>
<td width="19%">Name</td>
<td width="81%"><label>
<input name="txtname" type="text" id="txtname" />
</label></td>
</tr>
<tr>
<td>Age</td>
<td><label>
<input name="txtage" type="text" id="txtage" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="submit" type="submit" id="submit" value="submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>setcookie('Username','MyName')If(isset($_COOKIE['Username'])){
echo $_COOKIE('Username');
}session_start() function. This function will send a cookie to the Web browser. Like set_cookie(), no white spaces or code or anything else should precede this function. Once a session is started, PHP sends a cookie to the browser with a name of PHPSESSID and a 22 character string. You can then assign values to the $_SESSION array like so:$_SEESION['age'] = 5
<?php
session_start();
$err ="";
if(isset($_POST['submit'])){
//check if the form values are not empty
if(empty($_POST['txtname'])){
$err.= "Please enter a name.";
}
if(empty($_POST['txtage'])){
$err.= "Please enter a age.";
}
if(strlen($err) < 1 ){
$_SESSION['uname'] = $_POST['txtname'];
$_SESSION['age'] = $_POST['txtage'];
echo "Session set";
}else{
echo "The following errors occurred: ".$err;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Maintaining State in PHP</title>
<style type="text/css">
<!--
.style1 {font-size: 14px}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="createsession.php">
<table width="100%" border="1">
<tr>
<td colspan="2"><h1> Create Session </h1></td>
</tr>
<tr>
<td width="19%">Name</td>
<td width="81%"><label>
<input name="txtname" type="text" id="txtname" />
</label></td>
</tr>
<tr>
<td>Age</td>
<td><label>
<input name="txtage" type="text" id="txtage" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="submit" type="submit" id="submit" value="submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
Click here for larger image
Figure 1. Result of Assigning Cookie Values
$_SESSION['uname'] = $_POST['txtname'];
$_SESSION['age'] = $_POST['txtage'];
<?php
session_start();
$nameAge="";
if(isset($_SESSION['uname'])){
$nameAge.= "Username is: <strong>".$_SESSION['uname']."</strong> ";
}
if(isset($_SESSION['age'])){
$nameAge.= "User Age: <strong>".$_SESSION['age']."</strong>";
}
echo $nameAge;
?>
Click here for larger image
Figure 2. Printout of Values from the Server
<?php
session_start();
//delete session variables
unset($_SESSION);
//delete session data
session_destroy();
echo "Session data removed";
?>
session_start() function to open up any session that is currently active and then uses the unset() function to delete session variables. The final function it then calls is the session_destroy() function; its purpose is to destroy any session data that is still remaining.