0

I have been trying to create a login page in PHP to no avail though, i have been getting a "This webpage has a redirect loop" error when i try to run it, so i was wondering if anyone could possibly spot my mistakes? Here is my code:

<?php
require_once("nocache.php");
$id = $_POST["id"];
$pword = $_POST["pword"];
if (!empty($_POST){
if (!empty($id) || !empty($pword)){
require_once("dbconn.php");
 $sql = "select username, school_type from school_info where username = '$id' and password = '$pword'";
 $rs = mysql_query($sql, $dbConn);
 if (mysql_num_rows($rs)> 0 ) {
     session_start();
     $_SESSION["who"] = $id;
     $_SESSION["school_type"] = mysql_result($rs,0,"school_type");
     header("location: EOI_home.php");
    }
}
else {
header("location: login.php");}     
} else {
   header("location: login.php");}

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="login">

ID: <input type="text" name="id" /><br/>
pword: <input type="password" name="pword" /><br/>

<input type="submit" value="log in" />&nbsp;
<input type="reset" />

</form>
AJJ
  • 899
  • 3
  • 11
  • 16

2 Answers2

1

All of the answers given are technically correct, the way you've set your logic is incorrect... take the following example and port it across into your own code.

<?php

$id = $_POST["id"];
$pword = $_POST["pword"];

if(!empty($_POST)) {
    // The form was submitted, perform validation here.

    if(!empty($id) || !empty($pword)) {
        // Form validation passed, insert into database
    } else {
        // Form validation failed, display an error or redirect back
    }
} else {
    // Form was not submitted, so display the form.
}

?>

Edit I was hoping not to have to do the work for you (since it's best you learn) but perhaps seeing the above code, and the below code you can learn from it that way?

<?php
require_once("nocache.php");

$id = $_POST["id"];
$pword = $_POST["pword"];

if(!empty($_POST)) {
    if(!empty($id) || !empty($pword)) {
        require_once("dbconn.php");
        $sql = "select username, school_type from school_info where username = '$id' and password = '$pword'";

        $rs = mysql_query($sql, $dbConn);

        if(mysql_num_rows($rs) > 0) {
            session_start();
            $_SESSION["who"] = $id;
            $_SESSION["school_type"] = mysql_result($rs, 0, "school_type");

            header("location: EOI_home.php");
        }
    } else {
        header("location: login.php");
    }
}
?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="login">
    ID: <input type="text" name="id" /><br/>
    pword: <input type="password" name="pword" /><br/>

    <input type="submit" value="log in" />&nbsp;
    <input type="reset" />
</form>
naththedeveloper
  • 4,503
  • 6
  • 36
  • 44
0

The loop is here:

$id = $_POST["id"];
$pword = $_POST["pword"];
if (empty($id) || empty($pword)){
    header("location: login.php"); }

If the $_POST values are not set, the redirect happens. Since you are not setting the values before the redirect, it happens again. And again. And again...

To correct this problem, display your form if the $_POST values are not set.

George Cummins
  • 28,485
  • 8
  • 71
  • 90