I'm trying to loop through the session. But I can't seem to get the expected results. I'm still trying to explore things. So please teach me a better way to do this. If you find my code unsecure or inappropriate. First I have this login form:
<form name="x" action="login.php" method="post">
Username:<input type="text" name="uname" value=""></input><br/>
Password:<input type="password" name="pword" value=""></input>
<input type="submit" value="login"></input>
</form>
And here's login.php which sets the session if the record is found on the mysql database:
<?php
require_once("conn.php");
$username=$_POST['uname'];
$pword=md5($_POST['pword']);
echo $username."<br/>";
echo $pword;
$check=mysql_query("SELECT * FROM users WHERE Uname='$username' AND Hpword='$pword'");
if(mysql_num_rows($check)==0){
header('Location:loginform.php');
}else{
session_start();
while($result=mysql_fetch_assoc($check)){
$_SESSION['uid'].=$result['ID'];
$_SESSION['uname'].=$result['Uname'];
}
}
?>
And here's the file which loops through the session:
<?php
session_start();
echo "Logged in users:<br/>";
foreach($_SESSION as $sir){
}
echo "User id: ". $_SESSION['uid']."<br/>";
echo "Username: ".$_SESSION['uname']."<br/>";
?>
I get this:
While I'm expecting to get something like this:
User id: 1 Username: yoh
User id: 2 Username: max
Yes, PHP supports arrays as session variables.
We can remove items from the array by using array_diff() function. $_SESSION[cart]=array_diff($_SESSION[cart],$prod_id);
$_SESSION is available only for the visitor who opens the page actually. (It would be nice to see everyone's $_SESSION variables, isn't it?)
You may want to store these $_SESSION vars in your db then loop through them.
Update:
last_seen
last_seen
value is smaller than now
- server's session lifetimeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With