Note that there are some explanatory texts on larger screens.

plurals
  1. POSession Handling in PHP 5
    primarykey
    data
    text
    <p>i've a problem with php session handling that i can't explain to myself. I'm studying php from scratch, and i can't figure out how to mantain a session live:</p> <p>This is my index page, where a user can login or register to the database visiting the right page, and then come back to see if he's logged in:</p> <p>Code:</p> <pre><code>Index &lt;?php session_start(); ?&gt; &lt;a href="register.php" target="_self"&gt; Register &lt;/a&gt; &lt;a href="login.php" target="_self"&gt; Login &lt;/a&gt; &lt;?php if(isset($_SESSION['login'])) { echo "Logged as: ".$_SESSION['nlogin']; ?&gt; &lt;form method="post" action="&lt;?php unset($_SESSION['login']) ?&gt;"&gt; &lt;input type="button" name="logOut" value="LogOut" /&gt; &lt;/form&gt; &lt;?php } else { echo "Please Register or Login"; } ?&gt; </code></pre> <p>In fact this work, because when i come back from login.php it says, "Logged as: Admin"</p> <p>But when i click on the link to get the login page, or register page again from the index page, i should get the same message, "Logged as...", but the session appear to be closed instead. :(</p> <p>here's login.php:</p> <pre><code>&lt;?php session_start(); include "dbConnect.php"; if(isset($_SESSION['login'])) { echo "Logged as: ".$_SESSION['nlogin']; // IT NEVER SHOW THIS MESSAGE } if(isset($_POST['submit']) &amp;&amp;(trim($_POST['submit']) == "Login")) { if(!isset($_POST['user']) || $_POST['user']=="") { echo "Attenzione inserire l'username."; } elseif(!isset($_POST['pwd'])||$_POST['pwd']=="") { echo "Attenzione inserire la password."; } else { $u = trim(filter_var($_POST['user'], FILTER_SANITIZE_STRING)); $u = str_replace(" ","_",$u); $p = trim(filter_var($_POST['pwd'], FILTER_SANITIZE_STRING)); $p = sha1($p); $istance = new dbHandle; $istance-&gt;connect(); $data = $istance-&gt;query("SELECT * FROM login WHERE username_login = '$u' AND password_login = '$p'"); if(mysql_num_rows($data) == 0) { echo "Failed"; echo "&lt;a href='index.php' target='_self'&gt; Go Back &lt;/a&gt;"; } else { echo "Logged"; $res = $istance-&gt;getdata($data); $_SESSION['login'] = $res-&gt;id_login; $_SESSION['nlogin'] = $res-&gt;username_login; echo "&lt;a href='index.php' target='_self'&gt; Go Back &lt;/a&gt;"; } } } else { ?&gt; Login &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; ... &lt;input name="user" type="text" size="20" required="required"/&gt; ... &lt;input name="pwd" type="password" size="20" required="required"/&gt; ... &lt;input type="submit" name="submit" value="Login"/&gt; &lt;/form&gt; &lt;form method="post" action="&lt;?php unset($_SESSION['login']) ?&gt;"&gt; &lt;input type="button" name="logOut" value="LogOut" /&gt; &lt;/form&gt; &lt;?php } $istance-&gt;disconnect(); ?&gt; </code></pre> <p>When i come back using the link above "Go Back" to the index page, it shows Logged as... but when i come back here again, it does not.</p> <p>So i assume my session were destroyed automatically? but why?</p> <p>Thanks, i appreciate your help.</p> <p>I forget to say that PHP.ini has </p> <blockquote> <p>session.cookie_lifetime</p> </blockquote> <p>set to "0"</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload