2
<?php
    include("includes/connections.php");

    if(isset($_POST['sign_in'])) {


        $password = mysqli_real_escape_string($con,$_POST['pass']);
        $email = mysqli_real_escape_string($con,$_POST['email']);


        $get_user = "SELECT * FROM users WHERE user_email = '$email' AND user_password = '$password'";
        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if ($check==1) {

            $_SESSION['user_email']=$email;
            echo "<script>window.open('welcome.php','_self');</script>";

        }
        else {

            echo "<script>alert('Password or email is not correct');</script>";
            echo "<script>window.open('index.php','_self');</script>";

        }
    }

?>

Its been one day that i started learning php. I am stuck here. The code seems to be fine but it doesnt start the session on signup or login. It says Undefined index: user_email.

<h1>welcome : <?php echo $_SESSION['user_email']; ?></h1>

Can anybody help?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Himakar
  • 185
  • 12
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – MrDarkLynx Feb 17 '17 at 07:29
  • [session_start()](http://php.net/manual/en/function.session-start.php) – Mohammad Feb 17 '17 at 07:32
  • _Hint:_ You need to have a [`session_start()`](http://php.net/manual/en/function.session-start.php) before using any sessions. – M. Eriksson Feb 17 '17 at 07:33
  • when i add the session_start() it gives me a notice saying session already started and ignoring it. – Himakar Feb 17 '17 at 07:35
  • 1
    If you're just staring with PHP, don't start with the bad habit of concatenating your queries. You should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – M. Eriksson Feb 17 '17 at 07:35
  • Put session_start() in the top of your page, before any output. – M. Eriksson Feb 17 '17 at 07:37

2 Answers2

0

You forgot to put session_start() at the top of the page.

Jim L
  • 351
  • 2
  • 13
0

In your code at the point where 'check = 1', you need to put this line of code: session_start();

When you get an error the session is already started, you need to put this line of code at top of the document: ob_start();

<?php
 ob_start(); //Put ob_start(); here on this line


include("includes/connections.php");

        if(isset($_POST['sign_in'])) {


        $password = mysqli_real_escape_string($con,$_POST['pass']);
        $email = mysqli_real_escape_string($con,$_POST['email']);


        $get_user = "SELECT * FROM users WHERE user_email = '$email' AND user_password = '$password'";
        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if ($check==1) {
            session_start(); //Put session_start(); on this line
            $_SESSION['user_email']=$email;
            echo "<script>window.open('welcome.php','_self');</script>";

        }
        else {

            echo "<script>alert('Password or email is not correct');</script>";
            echo "<script>window.open('index.php','_self');</script>";

        }
    }

?>