Cass
Cass

Reputation: 557

I cannot retrieve a session variable in a second PHP file

What I am trying to do is, I have an HTML page that is an admin login form. I want to pass the username to other pages to verify that the user is logged in before being allowed to access the functions of the called page.

I have this code in an PHP file that is included in an .html file:

session_start();
session_register('username');
$_SESSION['id']=10;
echo "Login session id is " .$_SESSION['id'];

unset($_SESSION['username']);
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    require_once 'config.inc.php';

    $connect = mysqli_connect($host_name, $user_name, $password, $database);

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {
        $uName = $_POST['name'];
        $_SESSION['username'] = $uName;
        echo $_SESSION['username'];

..... > more to the file but this displays the username just fine.

In the second file, a .html file with PHP inline in the file:

<?php

        session_start();

        echo "Add user session id is ";
        echo "<br>";
        echo $_SESSION['id'];
        echo "<br>";
        echo "Hello";
        echo "<br>";
        echo $_SESSION['username'];
        echo "<br>";
        echo $_SESSION;
        echo "<br>";
        echo $_SESSION['time'];
        echo "<br>";
        echo date('Y m d H:i:s', $_SESSION['time']);

The hard code text and the "echo date('Y m d H:i:s', $_SESSION['time']);" display fine. The line echo date('Y m d H:i:s', $_SESSION['time']); displays the wort Array.

Can anyone tell me why I can't get the value of username?

Upvotes: 0

Views: 461

Answers (2)

Dillon Burnett
Dillon Burnett

Reputation: 503

This is the simplest example i could come up with. I hope it helps.

HTML

<form action="signin.php" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="text" name="password"><br>
    <input type="submit">
</form>

PHP [signin.php]

<?php
    if($_POST['username'] == 'username') && $_POST['password'] == 'password')
    {
        session_start();
        $_SESSION['username'] = $username;
        header("location: admin.php");
    }
    else
    {
        header('location: error.php?msg=Invalid_Username';
    }
?>

PHP [error.php]

<?php
    $msg = '';
    if(isset($_GET['msg']))
    {
         if($_GET['msg'] == "Invalid_Username")
         {
            $msg = "Your username or password didn't match.";
        }
        else if($_GET['msg'] == "Invalid_Access")
        {
            $msg = 'You are not allowed on that page.';
        }
    }
    else
    {
        $msg = 'Unknown Error';
    }
?>
<html>
    <body>
        <h1>ERROR</h1>
        <p><?php echo $msg;?></p>
    </body>
</html>

PHP [admin.php]

<?php
    session_start();
    if($_SESSION['username'])
    {
       echo 'your signed in';
    }
    else
    {
        header('location: error.php?msg=Invalid_Access');
    }
?>

Upvotes: 0

Script47
Script47

Reputation: 14540

Your issue is as you said yourself,

In the second file, a .html file with PHP inline in the file:

PHP has to be in a .php file, not a .html file extension.

Edit 1:

As per PHP.net:

Warning This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

You should stop using it.

Edit 2:

You are also echoing $_SESSION,

echo $_SESSION;

that should be throwing an array to string conversion error if your error reporting is enabled and the file extension is .php.

Upvotes: 1

Related Questions