Night
Night

Reputation: 749

PHP Session Variable not staying Constant

I have an issue with using $_Session[], I am trying to set a constant variable which should be active across all my pages.

It becomes active when someone successfully logs in, on login.php ( $logged_in gets set to 1 )

Here is the code for action.php:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    if($logged_in==1) {
        $_SESSION['logged_in'] = 1;
        $_SESSION['logged_user'] = $username;
    }
?>
<head>

action.php is where all of the form processes are done, so $logged_in gets set in action.php.

Then I attempt to call $_SESSION['logged_user'] in index.php:

(link to code) http://pastebin.com/8BA3csUZ

But as you can see by looking at the code: I attempt to call it <?php echo $_SESSION['logged_in']; ?> but nothing happens, how come?

edit: Login code:

if (isset($_GET['do']) && $_GET['do'] === "login") {
$username = $_POST['username'];
$password_input = $_POST['password'];
$password = md5($password_input);

$result = mysqli_query($con,"SELECT * FROM users WHERE username='" . $username . "'");
while($row = mysqli_fetch_array($result)) {
if($password == $row['password']) {
echo "successful login";
$logged_in = 1;
}
else { echo "unsuccessful login";}
}
}

...and yes I am aware of md5 not really doing much ;)

Upvotes: 0

Views: 331

Answers (1)

Alex
Alex

Reputation: 132

if (isset($_GET['do']) && $_GET['do'] === "login") {
$username = $_POST['username'];
$password_input = $_POST['password'];
$password = md5($password_input);

$result = mysqli_query($con,"SELECT * FROM users WHERE username='" . $username . "'");
while($row = mysqli_fetch_array($result)) {
if($password == $row['password']) {
session_start();
$_SESSION['logged_in'] = 1;
echo "successful login";
}
else { echo "unsuccessful login";}
}
}

Upvotes: 1

Related Questions