Susmit Mohan
Susmit Mohan

Reputation: 23

"Notice: Undefined index..." error on web server but works perfectly on localhost

I only have two files, but get Notice: Undefined index... error with the session when running on web server, but it works perfectly on localhost

login.php:

    <?php 
    session_start(); 
    header("Location: index.php");
    exit;

    <form action="#" method="post">
    <input type="password" name="password" placeholder="Enter your password" >
    <input type="submit" name="submit" value="SUBMIT">
    </form>

    <?php
    $pw = (isset($_POST['password']) ? $_POST['password'] : null);
    $sb = (isset($_POST['submit']) ? $_POST['submit'] : null);

    if($pw == "8000"){
    $_SESSION['logged_in'] = 'green';
    }

    if($pw != "8000" && $sb != null){
    echo '<div class="warning">Password Incorrect !!</div>';
    exit;
    }

index.php:

    <?php
    session_start(); 
    if(!isset($_SESSION['logged_in']) && $_SESSION['logged_in']!= 'green')
    {  
    header('Location: login.php');
    exit;
    }
    include 'db.php';

After the PHP part, HTML code runs without having any whitespace.

When I try to access the index.php file before starting a session, the webpage shows this error:

Notice: Undefined index: logged_in in /bla/bla/bla/bla/public_html/index.php on line 3

Though this error is not present on localhost.

Upvotes: 0

Views: 1160

Answers (2)

DEarTh
DEarTh

Reputation: 1005

Your if condition is not correct you need to do this like

first you should check if array key exist then you should compare it with something.

if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in']!= 'green')
{  
header('Location: login.php');
exit;
}

UPDATE : Remove this

header("Location: index.php");
exit;

And add this to your login page

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']== 'green')
{  
    header('Location: index.php');
    exit;
}

This should help you.

Upvotes: 1

James
James

Reputation: 4783

I normally don't answer questions like this as with a little trial and error you could have solved it :) But hopefully this will show you a good beginner path to go down for future coding (then you move on to classes and better things).

With this code you separate all the things into new files or functions. This makes your code tidy, easy to read, easy to maintain, and moreso easy to reuse the same things in other places of your codebase.

Such as you might need to set the sessions in some place other than login.php. This approach is already used below, as the isLoggedIn() function is used in login.php and index.php.

E.g. If your login value needs to ever change (such as "green" to "red") then you change the value in getSessionSecurityValue() and isLoggedIn() and setLoginSession() functions are auto changed.

NOTE: I have not tested this code, but should work as long as you put the things in files as below.

 

login.php:

require_once 'initialise.php';
require_once 'isLoggedIn.php';
require_once 'isPasswordValid.php';
require_once 'setLoginSession.php';

// If logged in no need to do the form thing
if (isLoggedIn() === true) {
    // Redirect to index
    // Or echo message "already logged in" and exit so not to show the login form
}

// Initialise vars
$errorMessage = null;
$postPassword = null;

// If form submitted
if (isset($_POST['submit'])) {

    $postPassword = $_POST['password'];
    $passwordValid = isPasswordValid($postPassword);

    // Password is valid, set their session and send them to index page
    if ($passwordValid === true) {
        setLoginSession();
        header('Location: index.php');
        exit;
    }

    // Password not valid (would have redirected above otherwise)
    $errorMessage = '<div class="warning">Password Incorrect !!</div>';
}

// If we have an error message show it
if ($errorMessage !== null) {
    echo $errorMessage;
}

?>
    <form action="#" method="post">
        <input type="password" name="password" placeholder="Enter your password">
        <input type="submit" name="submit" value="SUBMIT">
    </form>

 

index.php:

require_once 'initialise.php';
require_once 'isLoggedIn.php';

// If not logged in send to login page
if (isLoggedIn() === false) {
    header('Location: login.php');
    exit;
}

// They're logged in, good to go...
include 'db.php';

 

isPasswordValid.php:

/**
 * Check the entered password.
 *
 * @param string $password
 *
 * @return bool
 */
function isPasswordValid($password)
{
    return $password == '8000' ? true : false;
}

 

isLoggedIn.php:

require_once 'getSessionSecurityValue.php';

/**
 * Check if the user is logged in.
 *
 * @return bool
 */
function isLoggedIn()
{
    return (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == getSessionSecurityValue())
        ? true
        : false;
}

 

initialise.php:

/** Add startup and other shared things in here that are relevant to some initialisation */

// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

 

setLoginSession.php:

require_once 'getSessionSecurityValue.php';

/**
 * Sets the session to show user is logged in.
 *
 * @return void
 */
function setLoginSession()
{
    $_SESSION['logged_in'] = getSessionSecurityValue();
}

 

getSessionSecurityValue.php:

/**
 * Gets the "security" value used in the login session.
 *
 * @return string
 */
function getSessionSecurityValue()
{
    return 'green';
}

Upvotes: 0

Related Questions