Home>

Isn't you jumping to index2.php, which is set after pressing the login button, but you just go back to this page again? Do you know what the cause is?

Membership registration and the contents are saved in the DB. If the address and password entered on the login page match the contents of the DB, it will be transferred to the page after pressing the login button.

I ’m following the textbook,
I do not know what is wrong, thank you.

Applicable source code
<? php
session_start ();
require ("dbconnect.php");

# If ($_ COOKIE ["name"]! == "") {
# $name = $_ COOKIE ["name"];
#}

if (! empty ($_ POST)) {
    if ($_ POST ["email"]! == ""&&$_POST ["password"]! == "") {
    $login = $db->prepare ("select * from members where email =?
    and password =? ");

    $login->execute (array (
     $_POST ["email"],
     sha1 ($_ POST ["password"])
    ));
    $member = $login->fetch ();
    if ($member) {
      $_SESSION ["id"] = $member ["id"];
      $_SESSION ["time"] = time ();
    #if ($_ POST ["save"] === "on") {
     # setcookie ("name", $_ POST ["name"], time () + 60 * 60 * 24 * 14);
    #}
      header ("Location: index2.php");
      exit ();
    } else {
        $error ["login"] = "failed";
    }
    } else {
        $error ["login"] = "blank";
    }
    }

?>
Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

php
  • Answer # 1

    Check which branch you are going through and see if each variable has the expected information.

    Example:

    prepare ("select * from members where email =?
        and password =? ");
        $login->execute (array (
         $_POST ["email"],
         sha1 ($_ POST ["password"])
        ));
        $member = $login->fetch ();
        echo "dump contents of member" .PHP_EOL;
        var_dump ($member);
        if ($member) {
        echo __LINE __. PHP_EOL;
          $_SESSION ["id"] = $member ["id"];
          $_SESSION ["time"] = time ();
        #if ($_ POST ["save"] === "on") {
         # setcookie ("name", $_ POST ["name"], time () + 60 * 60 * 24 * 14);
        #}
          //header("Location:index2.php ");
        echo __LINE __. PHP_EOL;
          exit ();
        } else {
        echo __LINE __. PHP_EOL;
            $error ["login"] = "failed";
        }
        } else {
        echo __LINE __. PHP_EOL;
            $error ["login"] = "blank";
        }
        echo __LINE __. PHP_EOL;
        }
    echo __LINE __. PHP_EOL;
    ?>

  • Answer # 2

    In index2.php, check if $_SESSION ['id'] is set using echo etc.
    Maybe there is no session set up and you are redirected from index2.php

Related articles