简体   繁体   中英

redirecting user to another page using header()

I've looked everywhere and tried all solutions but it's still not getting me anywhere. Here's my code:

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))

{


  header('Location: /public_html/index.php');
  exit;

}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
    $username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));

    $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

    if(mysql_num_rows($checklogin) == 1)
    {
        $row = mysql_fetch_array($checklogin);
        $email = $row['EmailAddress'];

        $_SESSION['Username'] = $username;
        $_SESSION['EmailAddress'] = $email;
        $_SESSION['LoggedIn'] = 1;

        header('Location: /public_html/index.php');
        exit;
    }
    else
    {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
    }
}

    ?>

When I upload this into my server and login it brings me to a blank php page. What am I doing wrong with this code? I've tried using " instead of ' . I'm trying to redirect the user to a page outside of the folder hence the "/public_html/". Any help would be much appreciated.

如果在文件夹之外而不是使用

header("Location:../public_html/index.php");

Starting your URL with / and followed with the folder name means, you want to go to the top/root directory, and followed with the folder name you've specified.

For example: Getting to /public_html means you're redirecting to http://www.example.com/public_html which I believe to be non-existance in your case

To get around this problem, do this: add a double dot: ..

Getting to ../public_html means you're redirecting from http://www.example.com/some_project/some_folder/ to http://www.example.com/some_project/public_html/ (for example).

refer to Vicky's answer,

header("Location:../public_html/index.php");
exit();

and do not forget to add the exit(); in the end.


edit: Please cease using the ancient PHP-MySQL. There's a better alternative for you to use, for your own safety:

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