Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple PHP Session variables
    primarykey
    data
    text
    <p>I can't figure out why the following PHP won't work properly:</p> <pre><code> &lt;?php //Authenticate user credentials function authenticate($name, $password) { $sql = "SELECT * FROM users WHERE username = '$name' AND password = '" . md5($password) . "'"; $result = mysql_query($sql); if ($row = mysql_fetch_array($result)) { //Successful login echo "Welcome back, " . $row['username'] . "!"; return true; } else { //Failed login echo "Invalid username or password"; return false; } } function addUser($name, $password) { $sql = "INSERT INTO users (username, role, password) VALUES ('$name', 'CHAIR', '". md5($password) . "');"; mysql_query($sql); } function createTables() { $sql = "CREATE TABLE Users ( Create table Users( username varchar(50) NOT NULL, role ENUM('CHAIR', 'FACULTY', 'STAFF') NOT NULL, password varchar(255) NOT NULL, email varchar(50) NOT NULL PRIMARY KEY, applicant_client_id varchar(50) NOT NULL, countries varchar(50), research_area varchar(255), numAssignedReviews TINYINT UNSIGNED, available varchar(50), workload FLOAT UNSIGNED );" ; mysql_query($sql); } session_start(); if (isset($_SESSION['loggedIn'])) { echo '&lt;script&gt; alert("LoggedIn is set") &lt;/script&gt;'; $con = mysql_connect("localhost", "ttony21_Tony", "cse308"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ttony21_GARS", $con); $name = $_POST["name"]; $password = $_POST["password"]; if (authenticate($name, $password)) { $_SESSION['loggedIn'] = true; } mysql_close($con); } else { $_SESSION['loggedIn'] = false; echo '&lt;script&gt; alert("LoggedIn is not set") &lt;/script&gt;'; } ?&gt; &lt;!DOCTYPE html&gt; &lt;html lang="en-US"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8" /&gt; &lt;title&gt;GARS&lt;/title&gt; &lt;style type="text/css"&gt; body { text-align: center; min-width: 600px; } #wrapper { margin: 0 auto; width: 600px; text-align: left; } &lt;/style&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class = "wrapper"&gt; &lt;?php if ($_SESSION['loggedIn']) { echo ' &lt;div class = "upload"&gt; &lt;form action="upload_file.php" method="post" enctype="multipart/form-data"&gt; &lt;label for="file"&gt;Filename:&lt;/label&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;br /&gt; &lt;input type="submit" name="submit" value="Submit" /&gt; &lt;/form&gt; &lt;/div&gt; '; } else { echo ' &lt;div class = "login"&gt; &lt;form action = "/" id = "loginForm" method = "post"&gt; &lt;p&gt; Username: &lt;input type = "text" name = "name" /&gt; &lt;br /&gt; Password: &lt;input type = "password" name = "password" /&gt; &lt;br /&gt; &lt;input type = "submit" id= "submit"/&gt; &lt;/p&gt; &lt;/form&gt; &lt;/div&gt; '; } ?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>There's a form: <code>&lt;form action = "/" id = "loginForm" method = "post"&gt;</code></p> <p>When I either submit the form, or refresh the page, it should alert me with the message "LoggedIn is set" correct? But I only get the message "LoggedIn is not set." </p> <p>I know that the session starts and the loggedIn is set properly, but I seem to lose the variable when I refresh the page or use the form, which shouldn't happen because it's a session variable right? </p> <p>I'm new to using them so any help would be appreciated.</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.
    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