Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql result to set session variable
    text
    copied!<p>I want to use the result of a mysql query to set a session variable however at present I am struggling to make it set.</p> <p>My code I have is:</p> <pre><code>&lt;?php ob_start("ob_gzhandler"); ?&gt; &lt;?php // Include required MySQL configuration file and functions require_once('config.inc.php'); require_once('functions.inc.php'); // Start session session_start(); // Check if user is already logged in if ($_SESSION['logged_in'] == true) { // If user is already logged in, redirect to main page redirect('../index.php'); } else { // Make sure that user submitted a username/password and username only consists of alphanumeric chars if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR (!ctype_alnum($_POST['username'])) ) { redirect('../login.php'); } // Connect to database $mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); // Check connection if (mysqli_connect_errno()) { printf("Unable to connect to database: %s", mysqli_connect_error()); exit(); } // Escape any unsafe characters before querying database $username = $mysqli-&gt;real_escape_string($_POST['username']); $password = $mysqli-&gt;real_escape_string($_POST['password']); // Construct SQL statement for query &amp; execute $sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'"; $result = $mysqli-&gt;query($sql); // If one row is returned, username and password are valid if (is_object($result) &amp;&amp; $result-&gt;num_rows == 1) { // Set session variable for login status to true $_SESSION['auth_lvl'] = $Auth_lvl; $_SESSION['logged_in'] = true; redirect('../index.php'); } else { // If number of rows returned is not one, redirect back to login screen redirect('../login.php'); } } ?&gt; </code></pre> <p>I am then testing the session variables with:</p> <pre><code>&lt;?php session_start(); echo "Login Status is:".$_SESSION['logged_in']; echo "&lt;br/&gt;"; echo "Auth status is level:".$_SESSION['auth_lvl']; ?&gt; </code></pre> <p>On my testing page the session logged in works fine displaying the correct information however the auth lvl variable displays nothing.</p> <p>I can only assume that I am not calling the information correctly that I am setting the variable with in the first place.</p> <p>Any help would be appreciated.</p> <p>Alan.</p> <p>Something I have notice and I have been trying the suggestions is that if I set a definative rather than trying to retreive a value from the database that value will return on my test page. i.e. </p> <pre><code>$_SESSION['auth_lvl'] = 'test'; </code></pre> <p>This tells me it is the way in which I am pulling the data from the database and trying to set it as $_SESSION['auth_lvl'] that is causing the problem.</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