PHP/Cookie Session/ SESSION
Содержание
- 1 check for a valid login in sessions
- 2 Counting page accesses with a session
- 3 Count Visits with session
- 4 You have visited this page <?php echo( $_SESSION["count"] ); ?> times in this session
- 5 create_session_variable.php
- 6 delete_session_variable.php
- 7 Doing something special for a logged in user
- 8 Implementing Sessions
- 9 Printing session data
- 10 Registering a variable by including it in $_SESSION
- 11 Session based counter
- 12 Session preferences
- 13 Session using the proper $_SESSION super global
- 14 Setting Up Cookie Authentication
- 15 Verifying session info
check for a valid login in sessions
<source lang="html4strict">
<?php if ($_SESSION["loggedin"]){ echo "Proper login"; } else { echo "You are not logged in."; } ?>
</source>
Counting page accesses with a session
<source lang="html4strict">
session_start(); $_SESSION["count"] = $_SESSION["count"] + 1; print "You"ve looked at this page " . $_SESSION["count"] . " times.";
</source>
Count Visits with session
<source lang="html4strict">
<?php
session_start(); #start a session if ( !isset( $_SESSION["count"] ) ) $_SESSION["count"] = 1; else $_SESSION["count"]++;
?>
<html> <head> <title>Count Visits</title> </head> <body>
You have visited this page <?php echo( $_SESSION["count"] ); ?> times in this session
</body> </html>
</source>
create_session_variable.php
<source lang="html4strict">
<?php
session_start(); $_SESSION["username"] = "jason"; echo "Your username is ".$_SESSION["username"].".";
?>
</source>
delete_session_variable.php
<source lang="html4strict">
<?php
session_start(); $_SESSION["username"] = "jason"; echo "Your username is: ".$_SESSION["username"].".
"; unset($_SESSION["username"]); echo "Username now set to: ".$_SESSION["username"].".";
?>
</source>
Doing something special for a logged in user
<source lang="html4strict">
<?php session_start(); if (array_key_exists("username", $_SESSION)) {
print "Hello, $_SESSION[username].";
} else {
print "Howdy, stranger.";
} ?>
</source>
Implementing Sessions
<source lang="html4strict">
<?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.
} ?>
</source>
Printing session data
<source lang="html4strict">
<?php session_start(); $main_dishes = array("cuke" => "A",
"stomach" => "B", "abalone" => "C");
if (count($_SESSION["order"]) > 0) {
print "- ";
foreach ($_SESSION["order"] as $order) {
$dish_name = $main_dishes[ $order["dish"] ];
print "
- $order[quantity] of $dish_name ";
}print "";
} else {
print "You haven"t ordered anything.";
} ?>
</source>
Registering a variable by including it in $_SESSION
<source lang="html4strict">
<?php session_start( ); $_SESSION["hello"] = "Hello World"; echo $_SESSION["hello"]; ?>
</source>
Session based counter
<source lang="html4strict">
<?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>
PHPSESSID = <?php echo session_id(); ?>
You have been here <?php echo( $_SESSION["count"] ); ?> times in this session </body>
</html>
</source>
Session preferences
<source lang="html4strict">
<?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>
Select Your Preferred Font Family
<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
<input type="submit" value="Submit"> </form> </body>
</html>
</source>
Session using the proper $_SESSION super global
<source lang="html4strict">
<?php session_start( ); if (isset($_SESSION["username"])) {
$username=htmlentities($_SESSION["username"]); echo "Hello $username";
} else {
echo "Please login.";
} ?>
</source>
Setting Up Cookie Authentication
<source lang="html4strict">
<?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 ()) {
?>
you are logged in!
<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>
</source>
Verifying session info
<source lang="html4strict">
<?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."; }
} ?>
</source>