Malcolm Salvador
Malcolm Salvador

Reputation: 1566

Session variable still undeclared between pages

I have created a LoginPage.php with the following code

<?
session_start();
if (isset($_SESSION['uid']))
{
    header('Location: index.php');          
}   
?>
 <!DOCTYPE html>
  <html>
   <head>
    <?php 
    include("indexhead.php"); 
    ?>
    </head>
<body>
    <?php
        include("myCarousel.php");
        echo "
        <div class='container'>
            <div class='row'>
                <div class='col-sm-6 col-md-4 col-md-offset-4'>

                 <h1 class='text-center login-title'>Sign in to continue:</h1>
                    <div class='account-wall'>

                        <form class='form-signin' method='POST'>

                            <input name='uid' type='text' class='form-control' 
                            placeholder='Username' required autofocus >
                            <input name='pass' type='password' class='form-control' 
                            placeholder='Password' required >

                            <button class='btn btn-lg btn-primary btn-block' type='submit'>
                            Sign in</button>

                        </form>
                    </div>

                </div>
            </div>
        </div>
         ";

    if (isset($_POST['uid']) and isset($_POST['pass']))
    {
        if (empty($_POST['uid']) or empty($_POST['pass']))
        { 
            echo "Please type data into the login";                 
        }
        else
        {
                $uname = stripslashes($_POST['uid']);
                $pass = stripslashes($_POST['pass']);
                $nick = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname.
                "' and pass = md5('".$pass."');");
                if ($nick != '')
                {
                    $_SESSION['Nick1'] = selectSpecific("select nickName from hera.LoginTable where uid ='".$uname.
                    "' and pass = md5('".$pass."');");

                    $_SESSION['FullName'] = selectSpecific("select FullName from hera.LoginTable where uid ='".$uname.
                    "' and pass = md5('".$pass."');");

                    $_SESSION['dept'] = selectSpecific("select dept from hera.LoginTable where uid ='".$uname.
                    "' and pass = md5('".$pass."');");

                    $_SESSION['accesslevel'] = selectSpecific("select accesslevel from hera.LoginTable where uid ='".$uname.
                    "' and pass = md5('".$pass."');");

                    $_SESSION['uid'] = selectSpecific("select uid from hera.LoginTable where uid ='".$uname.
                    "' and pass = md5('".$pass."');");


                    header("Location: index.php");
                    //echo $_SESSION['Nick1'];

                }
                else
                {
                    echo "Unknown Username/Password";
                }
        }   
    }   
 ?> 
 </body>
</html>

what it does is to check the login and when the login is successful the user is lead to the index.php of the website

however, The index.php page does not detect the $_SESSION[''] variables, namely $_SESSION['uid'] it says : Notice: Undefined index: uid in C:\xampp\htdocs\Hera\index.php on line 14

But I can confirm that the session variable does receive content from function selectSpecific(). Why is it not being held by the session?

here is the code for the index.php

<?php
    session_start();

    echo $_SESSION['uid'];

?>

Upvotes: 0

Views: 211

Answers (3)

Malcolm Salvador
Malcolm Salvador

Reputation: 1566

I simply forgot to put <?php on the very top of my LoginForm.php file.

Upvotes: 0

Vivek Singh
Vivek Singh

Reputation: 2447

first check your session value and than change your html like below you have not given name to submit button

 <form class='form-signin' method='POST'>

                            <input name='uid' type='text' class='form-control' 
                            placeholder='Username' required autofocus >
                            <input name='pass' type='password' class='form-control' 
                            placeholder='Password' required >

                            <input class='btn btn-lg btn-primary btn-block' type='submit' name='submit'/>

than change your php's if condition like below

     if(isset($_POST['submit']))
         {
       if(isset($_POST['uid']) && isset($_POST['pass']))
        {
            if (empty($_POST['uid']) || empty($_POST['pass']))
            { 
                echo "Please type data into the login";                 
            } else
                 {       
                $uname = stripslashes($_POST['uid']);
                $pass = stripslashes($_POST['pass']);

         $nick =mysqli_query("select * from `hera.LoginTable` where `uid` ='".$uname."' and `pass` = '".md5($pass)."'","$dbcon");
  $rowcount=mysqli_num_rows($nick);
                if ($rowcount==1)
                {
                $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

                    $_SESSION['Nick1'] = $row['Nick1'];

                    $_SESSION['FullName'] = $row['FullName'];

                    $_SESSION['dept'] = $row['dept'];

                    $_SESSION['accesslevel'] = $row['accesslevel'];

                    $_SESSION['uid'] = $row['uid'];

                    header("Location: index.php");

                }
                else
                {
                    echo "Unknown Username/Password";
                }
        }   
    }   }
    ?> 

and using multiple query why don't use a single query like below

"select * from `hera.LoginTable` where `uid` ='".$uname.
                    "' and `pass` = '".md5($pass)."'");

Upvotes: 0

Matt D.
Matt D.

Reputation: 591

I would suggest breaking this down to the simplest possible iteration and just have 1 page with session initialize

<?php
    session_start();

    $_SESSION['uid'] = 12345;

and another with the echo.

<?php
session_start();

var_dump($_SESSION);

If that comes up empty then something is going on with the PHP session handler (lots of good info here).

Also make sure a session is established. By default it should use cookies (session.use_cookies = 1 in php.ini), so you should see a "PHPSESSID" cookie set. Without that there is no way to actually have a session.

Upvotes: 1

Related Questions