在制作登录页面时,我无法重定向到另一个页面

时间:2017-07-14 04:05:22

标签: php

将页面从登录重定向到索引时出错(即服务器错误//错误500)。我使用redirect_to函数从login.php文件调用function.php,我在function.php文件中包含了header函数。不幸的是,有服务器错误。我试图解决它,但我不能。我已经发布了我的所有四个文件。

的login.php

    <?php  
    require_once("../../includes/function.php");
    require_once("../../includes/database.php");
    require_once("../../includes/session.php");
    require_once("../../includes/user.php");

    if($session->is_logged_in()){
         redirect_to("index.php");
    }
    //remember to give your form's submit tag a name= "submit" attribute
    if(isset($_POST['submit'])){

    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    //check database to see if username/password exit.
    $found_user =  User::authenticate($username,$password);

    if($found_user){

        $session->login($found_user);
         redirect_to("index.php");
    }else{

        //username/password combo was not found in the database
         $message ="Username/password incorrect.";
          echo  $message;

    } 
    }
    else{//form has not been submitted

    $username = "";
    $password = ""; 
    }
    ?>

    <?php if(isset($database)) {
        $database->close_connection();
    }
    ?>
    <html>
    <head>
        <title>Photo Gallery</title>
        <link href="boot/css/bootstrap.css" media="all" rel ="stylesheet" type 
    ="text/css"/>
    </head>

    <body>
     <div id="header">
    <h1>Photo Gallery</h1>
     </div>
     <div id ="main">
     <h2>staff login</h2>

     </div>


     <form action="login.php" method="post">
     <table>
     <tr>
        <td>Username:</td>
        <td>
        <input type = "text" name = "username" maxlength="30" value="<?php echo 
     htmlentities($username);?>"/>
        </td>   
     </tr>

     <tr>
        <td>Password:</td>
        <td>
            <input type = "password" name= "password" maxlength = "30" value ="
     <?php echo htmlentities($password);?>"/>
        </td>
     </tr>

     <tr>
        <td>
            <input type="submit" name="submit" value = "login"/>
        </td>
     </tr> 
     </table>
    </form>
    </body>
    </html> 

的index.php

    <?php 

    require_once('../../includes/function.php');
    require_once('../../includes/session.php');
    if(!$session->is_logged_in()) {
         redirect_to("login.php");
    }
    ?>

    <html>
    <head>
        <title>Photo Gallery</title>
        <link href="boot/css/bootstrap.css" media="all" rel ="stylesheet" type 
    ="text/css"/>
    </head>

    <body>
     <div id="header">
    <h1>Photo Gallery</h1>
     </div>
     <div id ="main">
     <h2>staff login</h2>
    </div>

    <div id = "footer">Copyright<?php echo date("Y", time());?>,prayash 
    bhari</div>
    </body>
    </html> 

function.php

    <?php
    ob_start();
      function strip_zeros_from_data($marked_string =""){
        //first remove the marked zeros
        $no_zeros = str_replace('*0','',$marked_string);
        //then remove any remaining marks
        $cleaned_string = str_replace('*','', no_zeors);
        return $cleaned_string;
      }
    function redirect_to($location = NULL){
        if ($location != NULL){
            header("Location : {$location}");
            exit;
        }

    }

    function output_message($message = ""){
        if($empty($message)){
            return "<p class = \"message\">{$message}</p>";
        }
        else{
            return "";
        }
    }
    function __autoload($class_name){
        $class_name = strtolower($class_name);
        $path = "../includes/{$class_name}.php";
        if(file_exists($path)){
            require_once($path);
        }else{
            die("the file {$class_name}.php could not found.");
        }

    }
    ob_end_flush();
    ?>

sesssion.php

<?php
// A class to help work with Sessions
//In our case, primarily to mange logging users in and out

//keep in mind when working with sessions that it is generally
//inadvisable to store DB-relate objects in sessions
class Session{ 
private $logged_in = false;
public $user_id;
function __construct(){

session_start();
$this->check_login();
if($this->logged_in){
    //actions to take right away if user is logged in
}else{
    //actions to take right away if user is not logged in
}
}  

public function is_logged_in(){
    return $this->logged_in; 
}


public function login($user){
    //database should find user based on username/password
if($user){
    $this->user_id = $_SESSION['user_id'] = $user -> id;
    $this->logged_in = true;

}


}

public function logout(){
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}

private function check_login(){
    if(isset($_SESSION['user_id'])){
        $this->user_id = $_SESSION['user_id'];
        $this->logged_id = true;

    }else{
        unset($this->user_id);
        $this->logged_in = false;
    }
}
}

$session  = new Session()
?>

error message

2 个答案:

答案 0 :(得分:0)

删除{}并将“..”放入

 header("Location : {$location}");

而不是

 header("Location:".$location);

答案 1 :(得分:0)

您必须重定向到完整网址,请尝试此操作,

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

redirect_to($url."index.php");

上面提到的Gyandeep也改变了你的功能。

function redirect_to($location){
        header('Location:'.$location);
        exit();
    }

希望这有帮助。