Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Php - Getting good with session

2.072 visualizaciones

Publicado el

Getting good with session

Publicado en: Tecnología
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

Php - Getting good with session

  1. 1. PHP Session [email_address]
  2. 2. Agenda <ul><li>What is session in PHP? </li></ul><ul><li>Why use session? </li></ul><ul><li>What are important special variables? </li></ul><ul><li>How to apply session in your web application? </li></ul><ul><li>Sample application. </li></ul>
  3. 3. What is Session in PHP? <ul><li>$_SESSION[‘variable’] </li></ul><ul><li>A special type of variable whose value is maintained across subsequent pages </li></ul><ul><li>Its value will be destroyed if </li></ul><ul><ul><li>The user closes the web browser, or </li></ul></ul><ul><ul><li>the maximum time allotment set on the server for session lifetime is exceeded, or </li></ul></ul><ul><ul><li>session_destroy() </li></ul></ul>
  4. 4. Why Session? <ul><li>the HTTP protocol used to browse the web is stateless </li></ul><ul><li>Each request for a page is completely independent of earlier requests </li></ul><ul><li>so if you want subsequent pages to &quot;remember&quot; the user’s name that he/she entered on your front page, you have to store that information somewhere. </li></ul>
  5. 5. Important Special Variables <ul><li>$_SESSION[] - PHP super global array variable that contains currently registered to a script's session. </li></ul><ul><li>session_start() - initialises session data. This function is called prior to creating a new session variable using $_SESSION[]. </li></ul><ul><li>session_destroy() - destroys all data registered to a script's current session. </li></ul><ul><li>session_id() - used to get the id value for the current session. </li></ul>
  6. 6. Redirecting to Another Page <ul><li>header(&quot;Location: http://www.domain.com&quot;) - header function used redirect the browser page to the Location parameter provided. </li></ul>
  7. 7. Applying Session: register a session <ul><li><?php </li></ul><ul><li>if (isset($_POST['submit'])) { </li></ul><ul><li>if ($_POST['username'] == 'admin' && $_POST['password'] == 'admin') { </li></ul><ul><li>session_start(); </li></ul><ul><li> $_SESSION['username'] = $_POST['username']; </li></ul><ul><li> header('Location:index.php'); </li></ul><ul><li>} else </li></ul><ul><li>echo &quot;You have entered wrong username and password&quot;; </li></ul><ul><li>} else { </li></ul><ul><li>echo &quot;Please enter valid username and password&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  8. 8. Applying Session: start session in subsequent pages <ul><li><?php </li></ul><ul><li>session_start(); </li></ul><ul><li>if ( !isset($_SESSION['username'] )) { </li></ul><ul><li>header('Location:login.php'); </li></ul><ul><li>} else { </li></ul><ul><li>echo &quot;<div align=&quot;right&quot;>&quot;; </li></ul><ul><li>echo &quot;login as: &quot; . $_SESSION['username']; </li></ul><ul><li>echo &quot; | <a href=&quot;logout.php&quot;>logout</a>&quot;; </li></ul><ul><li>echo &quot;</div>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  9. 9. Applying Session: Kill a Session <ul><li><?php </li></ul><ul><li>session_start(); </li></ul><ul><li>session_destroy(); </li></ul><ul><li>header('location:index.php'); </li></ul><ul><li>?> </li></ul>

×