How to loop through session array in php
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:

![enter image description here][1]


  [1]: http://i.stack.imgur.com/bq08u.png



While I'm expecting to get something like this:

User id: 1
Username: yoh

User id: 2
Username: max

 

How to loop through session array in php
===

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:

![enter image description here][1]


  [1]: http://i.stack.imgur.com/bq08u.png



While I'm expecting to get something like this:

User id: 1
Username: yoh

User id: 2
Username: max

 
