Note that there are some explanatory texts on larger screens.

plurals
  1. POLocal variable overwrite session variable in PHP
    primarykey
    data
    text
    <p>Hi I'm having some problem with a PHP page: I'm writing a little CMS using <a href="http://www.intranetjournal.com/articles/200407/ij_07_06_04a.html" rel="nofollow">this</a> tutorial. I manage to write a class I use to interact with menus and all is working well: I can insert, delete and get all items of the menu in a page where I can reorder them. When I started to write the same page for users, I encountered an issue: I'm using a Sentry class to validate users in each page: </p> <pre><code>require_once('../includes/Sentry.php'); $theSentry = new Sentry(); if (!$theSentry-&gt;checkLogin(1) ){ header("Location: index.php"); die(); } </code></pre> <p>Now: if I use this validation alone, the page is working well, but I need to query the database and extract all the user in the user_admin.php page:</p> <pre><code>require_once('../includes/DbUser.php'); $user_connector = new DbUser(); $all_users = array(); $all_users = $user_connector-&gt;getUserArray(); foreach($all_users as $id =&gt; $user){ echo " ... " }; </code></pre> <p>If I comment one of the two section, all is working well, but if I leave this code running together, the page is correctly created, but next time I run a page using the Sentry class, I'm redirected to login page with an error. The Sentry class use a Validator class to check credentials, and a method in this class is reporting an array input instead of a single value input.</p> <p>My question is: how can be possible that two different objects created from two different classes, can interact generating such a problem? I think you need the code of the two method:</p> <pre><code>class Sentry { ... function checkLogin($group=9,$user='',$pass='',$goodRedirect='',$badRedirect='') { // Include database and validation classes, and create objects require_once('DbConnector.php'); require_once('Validator.php'); $validate = new Validator(); $loginConnector = new DbConnector(); // If user is already logged in then check credentials if ($_SESSION['user'] &amp;&amp; $_SESSION['pass']){ // Validate session data if (!$validate-&gt;validateTextOnly($_SESSION['user'])){return false;} if (!$validate-&gt;validateTextOnly($_SESSION['pass'])){return false;} if ($_SESSION['gruppo'] &lt;= $group){ // Existing user ok, continue if ($goodRedirect != '') { header("Location: ".$goodRedirect) ; } return true; }else{ // Existing user not ok, logout //$this-&gt;logout(); header("Location: low_perm.php"); die; //return false; } // User isn't logged in, check credentials }else{ // Validate input if (!$validate-&gt;validateTextOnly($user)){return false;} if (!$validate-&gt;validateTextOnly($pass)){return false;} // Look up user in DB $getUser = $loginConnector-&gt;query("SELECT * FROM `utenti` WHERE `usr` = '".$user."' AND `psw` = PASSWORD('".$pass."') AND `gruppo` &lt;= ".$group." AND `attivo` = 1"); $this-&gt;userdata = $loginConnector-&gt;fetchArray($getUser); if ($loginConnector-&gt;getNumRows($getUser) &gt; 0){ // Login OK, store session details // Log in $_SESSION["user"] = $user; $_SESSION["pass"] = $this-&gt;userdata['pass']; $_SESSION["gruppo"] = $this-&gt;userdata['gruppo']; if ($goodRedirect) { header("Location: ".$goodRedirect); } return true; }else{ // Login BAD unset($this-&gt;userdata); if ($badRedirect) { header("Location: ".$badRedirect) ; } return false; } } } } </code></pre> <p>And this is the function to get the users:</p> <pre><code>class DbUser extends DbConnector{ ... function getUserArray() { while ($row = mysql_fetch_object($this-&gt;user_result)) { $this-&gt;users[$row-&gt;id] = $row; } return $this-&gt;users; } } </code></pre> <p>I know it's a difficult-to-explain question, so let me know if I need to specify something else... Thanks</p> <p>EDIT: The error is in Validator class and in this function (the line with preg_match() ):</p> <pre><code>function validateTextOnly($theinput,$description = ''){ $result = preg_match ("/^[A-Za-z0-9\ ]+$/", $theinput ); if ($result AND $theinput!=''){ return true; }else{ $this-&gt;errors[] = $description; return false; } } </code></pre> <p>GREAT NEWS: I found where the error was, but I'm not able to understand why this code doesn't work:</p> <pre><code>$user_connector = new DbUser(); $all_users = array(); $all_users = $user_connector-&gt;getUsers(); foreach($all_users as $id =&gt; $user){ ... } </code></pre> <p>The foreach statement is the point: when I'm using <code>$all_user</code> as <code>$id=&gt;$user</code>, it actually overwrite the content of $_SESSION['user'] with the last object used in the cycle (an "user" object). Anyone can explain me how a local variable can overwrite a session one? I would like to make it clear: I solve the problem (changing <code>$id =&gt; $user</code> with <code>$id =&gt; $userObj</code>), but I'm looking for an explanation. Thanks!</p>
    singulars
    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.
 

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