Note that there are some explanatory texts on larger screens.

plurals
  1. POsession expire on time in php
    text
    copied!<p>I am new to Php.I am developing a website and I have used session for login and logout code.I am just trying to know that how to expire my session after specific time.eg say 20 minutes of idle use. My login code:</p> <pre><code>&lt;?php ob_start(); include("config.php"); session_start(); // set timeout period in seconds $inactive = 1200; // check to see if $_SESSION['timeout'] is set if(isset($_SESSION['timeout']) ) { $session_life = time() - $_SESSION['start']; if($session_life &gt; $inactive) { session_destroy(); header("Location: logout.php"); } } $_SESSION['timeout'] = time(); if($_SERVER["REQUEST_METHOD"] == "POST") { $myusername=addslashes($_POST['username']); $mypassword=addslashes($_POST['password']); $sql="SELECT rid FROM register WHERE rname='$myusername' and rpass='$mypassword'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); @$active=$row['active']; $count=mysql_num_rows($result); if($count==1) { $_SESSION['myusername']=$myusername; $_SESSION['login_user']=$myusername; echo "&lt;script language=\"javascript\"&gt; window.location.assign('Home.php')&lt;/script&gt;"; } else { $error="Your Login Name or Password is invalid"; } } ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Login Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div style="margin:30px"&gt; &lt;form action="" method="post"&gt; &lt;label&gt;UserName :&lt;/label&gt;&lt;input type="text" name="username" required="required" class="box"/&gt;&lt;br /&gt;&lt;br /&gt; &lt;label&gt;Password :&lt;/label&gt;&lt;input type="password" name="password" required="required" class="box" /&gt;&lt;br/&gt;&lt;br /&gt; &lt;input type="submit" value=" Submit "/&gt;&amp;nbsp;&amp;nbsp;&lt;input type="reset" value="Reset"/&gt;&lt;br /&gt; &lt;br /&gt; Not a member yet..? &amp;nbsp;&amp;nbsp; &lt;a href="register.php"&gt;Register&lt;/a&gt;&lt;/li&gt; &lt;/form&gt; &lt;div style="font-size:11px; color:#cc0000; margin-top:10px"&gt;&lt;?php echo @$error; ?&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and logout code:</p> <pre><code>&lt;?php session_start(); if(session_destroy()) { header("Location: home.php"); } ?&gt; </code></pre> <p>This code runs fine but user can't login.</p>
 

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