PHP/Cookie Session/session start

Материал из Web эксперт
Перейти к: навигация, поиск

Encode all session data into a single string and return the result

 
<?php
   session_start();
   $_SESSION["username"] = "jason";
   $_SESSION["loggedon"] = date("M d Y H:i:s");
   
   $sessionVars = session_encode();
   echo $sessionVars;
?>



<?php session_start(); ?>

 
<html>
 <head>
  <title>Session still running</title>
  <style type = "text/css">
    body { font-family:<?php echo( $_SESSION["font"] ); ?>; }
  </style>
 </head>
 <body>
  <h3>Preferred font family is still
  <?php echo( $_SESSION["font"] ); ?> </h3>
  <a href="prefs1.php">Change font?</a>  
 </body>
</html>



Session running

 
<?php  session_start(); ?>

<html>
 <head>
  <title>Session running</title>
  <style type="text/css">
    body { font-family:<?php echo $_SESSION["font"]; ?> }
  </style>
 </head>
 <body>
 <h3>Preferred font family is 
 <?php echo $_SESSION["font"]; ?></h3>
 <a href="prefs3.php?<?php echo( SID ); ?>">Next page</a>
 </body>
</html>



session_start() checks to see if a session has started, and if not, it starts one.

 
Its syntax is: boolean session_start()
<?
session_start();
?>



Session starter

 
<?php session_start(); ?>
<html>
 <head>
  <title>Session starter</title>
 <head>
 <body>
 <a href="next.php?<?php echo( SID ); ?>">Next page</a>
 <hr>
 PHPSESSID = 
 <?php 
  echo ( session_id() ); 
 ?>
 </body>
 
</html>



Sessions with register_globals on or off in session_test.php

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