0

I have a login page and a dashboard page where the user will land after login (note: the dashboard page is in another folder) ....

login.php

    <?php
include 'dbcon.php';
if(isset($_POST['sdsubmit'])){
    $sdemail=$_POST['sdemail'];
    $sdpassword=$_POST['sdpassword'];
    $sql = "SELECT * FROM students WHERE email = ? && password= ?";
    $query=$db->prepare($sql);
    $query->execute(array($sdemail, $sdpassword));
    $row = $query->fetch(PDO::FETCH_NUM);
    if($row > 0){
        session_start();
        $sdemail=$_SESSION['sdemail'];
        header("Location: student/index.php");
        exit();
    }
    else{
        echo "<script>alert('Sorry, email or password is incorrect. Please try again');
</script>";
    }
}

form(same page in the login.php)

    <form id="login-form1" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" role="form" style="display: block;">
        <div class="form-group">
                 <input type="email" name="sdemail" id="sdemail" required="required" tabindex="1" class="form-control" placeholder="Email" value="">
        </div>
            <div class="form-group">
                <input type="password" name="sdpassword" id="sdpassword" tabindex="2" class="form-control" placeholder="Password">
            </div>
        <div class="form-group">
        <div class="row">
        <div class="col-sm-6 col-sm-offset-3">
                <input type="submit" name="sdsubmit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Login">
        </div>
    </div>
        </div>
</form>

dashboard(index.php)

<?php
session_start();
echo $_SESSION['sdemail'];

dbcon.php(database connectivity)

<?php
$errmsg_arr = array();
$errflag = false;
try{
    $db=new PDO('mysql:host=localhost; dbname=internshala', 'root', '');
} catch (Exception $ex) {
    echo "Sorry, cannot connect to database";
}

error

Notice: Undefined index: sdemail in C:\xampp\htdocs\Internshala\student\index.php on line 3

chris85
  • 23,846
  • 7
  • 34
  • 51

1 Answers1

0
session_start();
$sdemail=$_SESSION['sdemail']; // refer 1
$_SESSION['sdemail'] = $sdemail; // just replace the above line with this
In dashboard.php
if(isset($_SESSION['sdemail']))
{ echo $sdemail; }// replace this
  1. here you are assigning the value to $sdemail from session($_SESSION['sdemail'])
Satish51
  • 119
  • 6