How to skip session array in php

I am trying to complete a session. But I can’t get the expected results. I'm still trying to research things. So please teach me better. If you find my code insecure 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 is login.php that sets up the session if the entry is found in 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 is the file that goes 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

So far, I expect to get something like this:

User ID: 1 Username: yoh

User ID: 2 Username: max

+4
source share
5 answers

$ _ SESSION is available only to the visitor who actually opens the page. (It would be nice to see all the $ _SESSION variables, right?)

You might want to keep these $ _SESSION vars in your db, and then scroll through them.

Update:

  • create a session table where you can save your registered users
  • every time a logged-in user opens a page, increase the value (timestamp), for example last_seen
  • simultaneously check dead sessions (for example, delete all rows where last_seen less than now - server session time
+7
source

Besides the extremely correct fabrik answer, there are just a few lines in your code:

 foreach($_SESSION as $sir){ } 

this cycle obviously does nothing. you cannot get any output from code that doesn't output anything :)

In addition, if you want to save multiple values ​​in a session, for example, in a shopping cart, you must save in the array, not a long, concatenated string:

  $_SESSION['cart'][] =$result; 

will create an array named $ _SESSION ['cart'], which can be repeated as desired:

 foreach ($_SESSION['cart'] as $result){ echo "Item id: ".$result['id'].", name: ".$result['name']."<br>\n"; } 
+3
source
 session_start(); foreach ($_SESSION as $name => $value) { echo $name."=".$value."<br>"; } 
+1
source

UPDATE : not a numerical index.

In login.php

 while($result=mysql_fetch_assoc($check)){ $_SESSION[$result['Uname'].$result['ID']] = array( "uid"=>$result['ID'], "uname"=>$result['Uname'] ); } 

And in you foreach

 foreach($_SESSION as $uinfos){ echo "User id: ".$uinfos["uid"]."<br/>Username: ".$uinfos["uname"]."<br/>"; } 

Thus, you have only one entry for each user.

0
source

why don't you store it like

$ _ SESSION [$ uid] = $ name; or $ _SESSION [] = array ('uid' => $ uid, 'name' => $ name);

then you can just iterate over all the records.

 foreach($_SESSION as $uid=> $name){ .... } 

or

 foreach($_SESSION as $userArray){ $uid = $userArray['uid']; $name = $userArray['name']; } 
-1
source

Source: https://habr.com/ru/post/1344907/


All Articles