简体   繁体   中英

PHP - Unable to redirect from login page to home page

I'm facing the problem for my login system. The home page (index2.php) cannot be redirected from login page (index.php) . The login page doesn't redirect to home page when I clicked "Login" button although I entered the correct ID and Password.

Here's my code:

index.php

<?php
    session_start();
?>

<form action="checklogin.php" method="POST">
<div align="center">
    <table style="background-color:#9FF781" width="285" border="1">
        <tr>
            <td width="130"><div align="right"><strong>ID :</strong></div></td>
            <td width="159"><label for="admid"></label>
                <input type="text" name="admid" id="admid"></td>
        </tr>
        <tr>
            <td><div align="right"><strong>PASSWORD :</strong></div></td>
            <td><label for="admpwd"></label>
                <input type="password" name="admpwd" id="admpwd"></td>
        </tr>
        <tr>
            <td colspan="2"><div align="center">
                <button type="submit" name="login">Login</button>
            </div></td>
        </tr>
    </table>
</div>
</form>

checklogin.php

<?php 
    include('../../../Connections/fyp.php');
    session_start();
    $admid = $_POST["admid"];
    $admpwd = $_POST["admpwd"];
    $result = mysql_query("SELECT * FROM acc_login WHERE id = '$admid' AND pwd = '$admpwd'");
    $row = mysql_num_rows($result);

    if($admid != "" && $admpwd != "")
    {
        if ($row != "")
        {
            $_SESSION['loggedin'] = $admid;
            header("Location: index2.php");
        }
        else
        {
            echo "<script type='text/javascript'>alert('Invalid ID or Password! Please try again!')</script>";
            exit('<a href="index.php"><strong>Go Back</strong></a> to Login Page');
        }
    }
    else
    {
        echo "<script type='text/javascript'>alert('ID or Password field cannot be blank! Please try again!')</script>";
        exit('<a href="index.php"><strong>Go Back</strong></a> to Login Page');
    }
?>

index2.php

<?php
    include("session.php"); //check whether user was logged in.
?>

session.php

<?php
    include('../../../Connections/fyp.php');
    session_start();
    $verify = $_SESSION['loggedin'];
    $session = mysql_query("SELECT * FROM acc_login WHERE id = '$verify'");
    $row = mysql_fetch_array($session);
    $login_session = $row['id'];

    if(!isset($login_session))
    {
        header("Location: index.php");
    }
?>

if header already sent then header function not work correctly. so it happens use this.

<?php
include('../../../Connections/fyp.php');
session_start();
$verify = $_SESSION['loggedin'];
$session = mysql_query("SELECT * FROM acc_login WHERE id = '$verify'");
$row = mysql_fetch_array($session);
$login_session = $row['id'];

if(!isset($login_session))
{
    echo "<script>location.href='index.php'</script>";
}
?>

this is what i do when i need to redirect after sending header or even ob_end_flush(); has been called.

Have you tried an absolute url to your home page?

checklogin.php

header("Location: http://www.example.com/index2.php");

Modify your files directories as follow:

1- in your checklogin.php

include('fyp.php');

2- in your session.php

include('fyp.php');

And remove the last ?> PHP closing tag in your PHP files (it is a good practice), to avoid headers' issues, then place all your PHP files in the same folder, if your problem is solved, it means you need to carefully check the URL you are passing based on your project folder structure.

Did you try to debug your code , Please put die('debug'); (or any thing in die() function ) before header("Location: index2.php"); and check the output , this will help you to reach the actual problem. or also put exit(1); after header();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM