0

I have this login form set out that converts the $_POST data into $_SESSION data. I then use this data to determine whether or not the user should be logged in, however it isn't working. What am I doing wrong?

*** I checked my browser's cookies, and it has the session token stored (if that helps)

login.php:

<?php session_start(); ?>

<form action="page.php" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
        }

        ?>

</form>

page.php:

<?php session_start(); ?>

<?php

        $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
        $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

        $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


        if(($username == "username") && ($password == "password") && ($submit == "Submit")) {
            echo "You are logged in";
        } else {
            echo "Denied access";
        }
?>

I get this error on pages that include session_start();

enter image description here

According to this post, it is a bug caused by Chrome that is a misleading error report, and doesn't affect anything.

Community
  • 1
  • 1
Daniel
  • 3,115
  • 5
  • 28
  • 39

5 Answers5

2
<form action="page.php" method="post">

    Username: <input type="text" name="username" value="" /><br />
    Password: <input type="password" name="password" value="" /><br />
    <br />
    <input type="submit" name="submit" value="Submit" />


 </form>

page.php

 <?php
    session_start();
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $submit = $_POST['submit'];

        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['submit'] = $submit;
    }
    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password") && ($submit == "Submit"))        {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }
?>
Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56
AVM
  • 592
  • 2
  • 11
  • 25
1

when you give action on form submit to another page page.php. So, why you get form data on same page ?

Please try this it will work :

HTML Form :

<form action=page.php method="post">

    Username: <input type="text" name="username" value="" /><br />
    Password: <input type="password" name="password" value="" /><br />
    <br />
    <input type="submit" name="submit" value="Submit" />


 </form>

page.php :

 <?php
    session_start();
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $submit = $_POST['submit'];

        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['submit'] = $submit;
    }
    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password") && ($submit == "Submit"))        {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }
?>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

Add session_start(); at the top of the pages and fix the action.

<?php session_start();?>
<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

        ?>

</form>

And

<?php
        session_start();
        $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
        $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

        $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


        if(($username == "username") && ($password == "password") && ($submit == "Submit")) {
            echo "You are logged in";
        } else {
            echo "Denied access";
        }
?>
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

First of all you need to start the session using session_start() in form page.Also you need to use header to send session to the login.php. Use the code below

<?php session_start();?>
<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

        ?>

</form>

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
0

Your form sends a post to page.php. Therefore, you will need to move session conditional to page.php.

Alteratively, you could send the form to yourself and redirect afterwards:

<?php session_start();?>

<form action="" method="post">

        Username: <input type="text" name="username" value="" /><br />
        Password: <input type="password" name="password" value="" /><br />
        <br />
        <input type="submit" name="submit" value="Submit" />

        <?php

        if (isset($_POST['submit'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $submit = $_POST['submit'];

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['submit'] = $submit;
            header('page.php');
        }

    $username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";
    $password = isset($_SESSION["password"]) ? $_SESSION["password"] : "";

    $submit = isset($_SESSION["submit"]) ? $_SESSION["submit"] : "";


    if(($username == "username") && ($password == "password")) {
        echo "You are logged in";
    } else {
        echo "Denied access";
    }

        ?>

</form>