PHP/Cookie Session/ SESSION

Материал из Web эксперт
Версия от 07:01, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

check for a valid login in sessions

 
<?php 
if ($_SESSION["loggedin"]){ 
echo "Proper login"; 
} else { 
echo "You are not logged in."; 
} 
?>



Counting page accesses with a session

 
session_start();
$_SESSION["count"] = $_SESSION["count"] + 1;
print "You"ve looked at this page " . $_SESSION["count"] . " times.";



Count Visits with session

 
<?php
     session_start();  #start a session
    if ( !isset( $_SESSION["count"] ) ) 
     $_SESSION["count"] = 1; else $_SESSION["count"]++;
?>

<html>
<head>
<title>Count Visits</title>
</head>
<body>
<h2>You have visited this page <?php echo( $_SESSION["count"] ); ?> times in this session</h2>
</body>
</html>



create_session_variable.php

 
<?php
   session_start();
   $_SESSION["username"] = "jason";
   echo "Your username is ".$_SESSION["username"].".";
?>



delete_session_variable.php

 
<?php
   session_start();
   $_SESSION["username"] = "jason";
   echo "Your username is: ".$_SESSION["username"].".<br />";
   unset($_SESSION["username"]);
   echo "Username now set to: ".$_SESSION["username"].".";
?>



Doing something special for a logged in user

 
<?php
session_start();
if (array_key_exists("username", $_SESSION)) {
    print "Hello, $_SESSION[username].";
} else {
    print "Howdy, stranger.";
}
?>



Implementing Sessions

 
<?php
session_start ();
$GLOBALS ["user"] = "test";
$GLOBALS ["pass"] = "test";
function login($username, $password) {
  if (strcmp ( $username, $GLOBALS ["user"] ) == 0 && strcmp ( $password, $GLOBALS ["pass"] ) == 0) {
    $_SESSION ["user"] = $username;
    $_SESSION ["pass"] = md5 ( $password );
    return true;
  
  } else {
    return false;
  }
}
function logout() {
  unset ( $_SESSION ["user"] );
  unset ( $_SESSION ["pass"] );
  session_destroy ();
}
if (login ( "test", "test" )) {
  echo "Successfully logged in with user: " . $_SESSION ["user"] . " and pass: " . $_SESSION ["pass"];
} else {
  echo "Could not login.";
}
logout ();
if (isset ( $_SESSION ["user"] )) {
  echo $_SESSION ["user"]; //Outputs nothing. 
}
?>



Printing session data

 
<?php
session_start();
$main_dishes = array("cuke" => "A",
                     "stomach" => "B",
                     "abalone" => "C");
if (count($_SESSION["order"]) > 0) {
    print "<ul>";
    foreach ($_SESSION["order"] as $order) {
        $dish_name = $main_dishes[ $order["dish"] ];
        print "<li> $order[quantity] of $dish_name </li>";
    } 
    print "</ul>";
} else {
    print "You haven"t ordered anything.";
}
?>



Registering a variable by including it in $_SESSION

 
<?php
session_start(  );
$_SESSION["hello"] = "Hello World";
echo $_SESSION["hello"];
?>



Session based counter

 
<?php session_start(); 
($_SESSION["count"]) ? $_SESSION["count"]++ : $_SESSION["count"] = 1;
?>

<html>
 <head>
  <title>Session running</title>
 </head>
 <body>
 <a href="session_start.php?<?php echo( SID ); ?>">Go to previous page</a>
 <hr>
 PHPSESSID = <?php echo session_id(); ?>
 <br>
 You have been here <?php echo( $_SESSION["count"] ); ?> times in this session
 </body>
</html>



Session preferences

 
<?php  session_start();
  if($_POST["font"] != null)
  {  
    $_SESSION["font"] = $_POST["font"];
    header( "Location:prefs2.php?" . SID );
    exit();
  }
?>

<html>
 <head>
  <title>Session preferences</title>
 </head>
 <body>
  <h3>Select Your Preferred Font Family</h3>
  <form action = "<?php echo( $_SERVER["PHP_SELF"] ); ?>" method = "post">
   <input type = "radio" name = "font" value = "serif">Serif
   <input type = "radio" name = "font" value = "sans-serif">Sans
   <input type = "radio" name = "font" value = "monospace">Mono
   <input type = "radio" name = "font" value = "cursive">Cursive
   <input type = "radio" name = "font" value = "fantasy">Fantasy
   <br><br>
   <input type="submit" value="Submit">
  </form>
 </body>
</html>



Session using the proper $_SESSION super global

 
<?php
session_start(  );
if (isset($_SESSION["username"])) {
    $username=htmlentities($_SESSION["username"]);
    echo "Hello $username";
} else {
    echo "Please login.";
}
?>



Setting Up Cookie Authentication

 
<?php
session_start ();
?>
<html>
<title></title> 
<?php
$GLOBALS ["user"] = "test";
$GLOBALS ["pass"] = "test";
if (isset ( $_POST ["user"] ) && isset ( $_POST ["pass"] )) {
  if (strcmp ( $_POST ["user"], $GLOBALS ["user"] ) == 0. && strcmp ( $_POST ["pass"], $GLOBALS ["pass"] ) == 0) {
    $_SESSION ["user"] = $_POST ["user"];
    $_SESSION ["pass"] = $_POST ["pass"];
  } else {
    ?>
Sorry, you have entered an incorrect login.
<?php
  }
}
if ($_POST ["logout"] == "yes") {
  unset ( $_SESSION ["user"] );
  unset ( $_SESSION ["pass"] );
  session_destroy ();
}
function checkcookies() {
  if (strcmp ( $_SESSION ["user"], $GLOBALS ["user"] ) == 0. && strcmp ( $_SESSION ["pass"], $GLOBALS ["pass"] ) == 0) {
    return true;
  } else {
    return false;
  }
}
?> 
</head>
<body>
<?php
if (checkcookies ()) {
  ?> 
<p>you are logged in!</p>
<form action="index.html" method="post">
  <input type="hidden" name="logout" value="yes" /> 
  <input type="submit" value="Logout" /></form> 
<?php
  //Or else present a login form. 
} else {
  ?> 
<form action="index.html" method="post">Username:<input type="text"
  name="user" maxlength="25" /> Password:<input type="password"
  name="pass" maxlength="25" /> <input type="submit" value="Login" />
</form> 
<?php
}
?> 
</body>
</html>



Verifying session info

 
<?php
unset($username);
if (isset($_SESSION["login"])) {
    list($c_username,$cookie_hash) = explode(",",$_SESSION["login"]);
    if (md5($c_username.$secret_word) == $cookie_hash) {
        $username = $c_username;
    } else {
        print "You have tampered with your session.";
    }
}
?>