Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Cookies
This article contains handful PHP function ae_put_cookie for easy cookie setting and "Cookie Notepad" example which allow visitor to store text in his own browser cookie.
<?php
function ae_put_cookie($name, $value, $days=0)
{
     $cookie_host = preg_replace('|^www\.(.*)$|', '.\\1', $_SERVER['HTTP_HOST']);

     if (substr(strval($days), 0, 1) == 'f')
         $exp = 2147483640;
     else if (substr(strval($days), 0, 1) == 'r')
     {
         $exp = 1; $value = '';
     }
     else if ($days != 0)
         $exp = time() + intval($days)*86400;
     else
         $exp = 0;

     setcookie($name, $value, $exp, '/', $cookie_host);
}
?>
Here is an example, 'Cookie Notepad' which allows to store entered text as a cookie and edit it later:
<?php
error_reporting(E_ALL); // high level of error reporting

// copy-paste function ae_put_cookie here from above

if (strtolower($_SERVER['REQUEST_METHOD']) == 'post')
{
     if (isset($_POST['notepad']))
     {
         $days = isset($_POST['days'])?$_POST['days']:'';
         ae_put_cookie('notepad', $_POST['notepad'], $days);
         header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}");
     }
}

?>
<html><head><title>CookieNotepad</title></head>
<body>
<?php
if (!isset($_COOKIE['notepad']))
     echo "<b>Cookie for notepad is not set</b><br>";
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Text:<br>
<textarea rows="10" cols="60" name="notepad">
<?php
if (isset($_COOKIE['notepad']))
{
     // escape HTML tags and entities
     $s = str_replace('&', '&amp;', $_COOKIE['notepad']);
     $s = str_replace('<', '&lt;', $s);
     $s = str_replace('>', '&gt;', $s);
     echo $s;
}
?></textarea>
<br>
Third argument for ae_put_cookie:<br>
<input type="text" name="days" size="10"><br>
(empty - session cookie, 'f' - forever, 'r' - remove, integer &gt; 0 - number of days in future)
<br><br>
<input type="submit" value="create/save">
</form>
</body>
</html>
Job or extra money for students

Search Engine Rank of your blog or websites