PHP Cookies and Sessions Tutorials - php Form

Author Topic: PHP Cookies and Sessions Tutorials  (Read 1150 times)

Offline alex

  • Global Moderator
  • *****
  • Posts: 77
  • Karma: +19/-0
    • View Profile
PHP Cookies and Sessions Tutorials
« on: May 10, 2010, 06:31:09 AM »


index.php
Code: [Select]
<?php @session_start();
if(isset(
$_POST['Submit']))
{
setcookie("cookieuser"$_POST['username'], time()+3600); 
setcookie("cookiepass"$_POST['password'], time()+3600);
print_r ($_COOKIE);
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
print_r ($_SESSION);
echo
"<a href=\"members.php\">members</a>";
}else{
if(isset(
$_SESSION['username']) && isset($_SESSION['password'])){
echo 
$_SESSION['username'] . "-" $_SESSION['password'] ;
}else{
echo
"<strong style=\"color:red\">no session</strong>";
echo
" <a href=\"members.php\">members</a>";
}
?>

<form method="post" action="index.php">
<input type="text" name="username" ><br>
<input type="text" name="password" ><br>
<input type="submit" name="Submit" value="submit">
</form>
<?php
}
?>


members.php
Code: [Select]
<?php @session_start(); 
if(isset(
$_COOKIE['cookieuser']) && isset($_COOKIE['cookiepass'])){
$_SESSION['username'] = $_COOKIE['cookieuser'];
$_SESSION['password'] = $_COOKIE['cookiepass'];
echo
"cool " $_SESSION['username'];
echo
" <a href=\"destroysession.php\">destroysession</a>";
}else{
echo
"no session ";
echo
"<a href=\"index.php\">login</a>";
die();
}
?>

<h1>members area</h1>

destroysession.php
Code: [Select]
<?php @session_start(); 
print_r ($_COOKIE);
setcookie("cookieuser"""time()-3600); 
setcookie("cookiepass"""time()-3600);

if(isset(
$_SESSION['username']) && isset($_SESSION['password'])){
unset(
$_SESSION['username']);
unset(
$_SESSION['password']);
}else{
echo
"<strong>no session</strong> ";
}
$_SESSION = array();
session_destroy();
echo
"<a href=\"index.php\"> index.php</a>";   
?>