0

There is an error while redirecting the page from login to index(i.e server error // error 500). I used redirect_to function to call function.php from login.php file and i have included header function in function.php file. unfortunately, there is server error.I tried to solve it but i could not.i have posted my all four file.

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

Prayas Bhari
  • 55
  • 2
  • 9
  • https://stackoverflow.com/a/24928578/4216992 Read this. It possible due to some issues that need to filter it carefully. – Salehin Rafi Jul 14 '17 at 04:33

2 Answers2

0

remove {} and put".." in

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

instead of

 header("Location:".$location);
GYaN
  • 2,327
  • 4
  • 19
  • 39
  • Actually, i have already sent the location of index file to header function through redirect_to function (i.e header function is in function.php and redirect_to function is in login.php and session.php) and i have tried your way to by replacing redirect_to with header but it is still not working. thank you – Prayas Bhari Jul 14 '17 at 05:02
  • Is it going to that `redirect_to` function? – GYaN Jul 14 '17 at 07:25
  • yup,it is going to redirect_to function in function.php – Prayas Bhari Jul 14 '17 at 08:54
  • edited ... You need to apply changes in function.php – GYaN Jul 14 '17 at 10:32
  • In session class, the value of logged_in is not working why is it so? i have assigned value of logged_in as false and it remains same for whole program. – Prayas Bhari Jul 16 '17 at 04:10
0

You have to redirect to full URL so Try this,

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

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

As Gyandeep Mentioned above change your function,too.

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

Hope this helps.

Krunal Limbad
  • 1,533
  • 1
  • 12
  • 23