Joosep Sisas
Joosep Sisas

Reputation: 33

PHP wrong redirect

When Im on my localhost SQL, it works. But now when I uploaded it to my clients SQL, it doesent. When I log in, it supposed to redirect to edit.php, after it has checked from database whether the name and password are correct or not, but it stays blank. Im out of ideas what to do, because it does work on my localhost sql.

Where I login, login form, (login.php)

<?php

include_once("logikinnitus.php");

session_start();
if(isset($_SESSION['logged']))
{

    header('Refresh: 2; URL=http://www.heakohv.ee/fava/admin/edit.php');
    echo "olete juba sisse logitud!";

  }else {

    if (isset($_POST["submit"])){

    $name = $_POST["user"];
    $pass = $_POST["pass"];

    $object = new User();
    $object -> login($name, $pass);
}

?>

<html>
<head></head>
<body>

<form method="post" action="login.php">
    Username: <input type="text" name="user"/>
    Password: <input type="password" name="pass"/>
    <input type="submit" name="submit" value="login">
</form>

<?php 

}

  ?>

</body>
</html>

Where I check if the password and user are correct (logikinnitus.php)

<?php

include_once("connection.php");


Class User {

    private $db;

    public function __construct(){

        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }

    public function login($name, $pass){

        if(!empty($name) && !empty($pass)){

            $st = $this -> db -> prepare("select * from users where name=? and pass=?");
            $st -> bindParam(1, $name);
            $st -> bindParam(2, $pass);
            $st -> execute();

            if($st -> rowCount() == 1){

                session_start();
                $_SESSION['logged'] = true;
                echo "Olete edukalt sisse logitud";
                header('URL=http://www.heakohv.ee/fava/admin/edit.php');
                exit();

            }
            else{

                echo "Parool või kasutajanimi on vale";

            }

        }
        else{

            echo "Palun täida kõik väljad!";

        }

    }

}

?>

connect php, (connection.php)

<?php

class Connection{

    public function dbConnect(){
        return new PDO("mysql:host=host; dbname=name", "pass", "");
    }
}

?>

Warning I get when error report turned on.

Warning: session_start() [function.session-start]: open(/home/la02/16840949/tmp/sess_46e83b3c6e65b60a68dd14f525bd92a5, O_RDWR) failed: No such file or directory (2) in /home/la02/16840949/admin/login.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/la02/16840949/admin/connection.php:3) in /home/la02/16840949/admin/login.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/la02/16840949/admin/connection.php:3) in /home/la02/16840949/admin/login.php on line 6

Warning: Unknown: open(/home/la02/16840949/tmp/sess_46e83b3c6e65b60a68dd14f525bd92a5, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/la02/16840949/tmp) in Unknown on line 0

If I add session.save_path"folder" it doesnt bring even my login form up anymore... I see only blank page from the beginning, even no errors –

Any questions, feel free to ask.

Upvotes: 2

Views: 395

Answers (2)

gview
gview

Reputation: 15361

The warnings are telling you what the problem is--- the session files can not be read or written, as they appear to be writing them to your *nix user home directory.

Warning: session_start() [function.session-start]: open(/home/la02/16840949/tmp/sess_46e83b3c6e65b60a68dd14f525bd92a5, O_RDWR) failed: No such file or directory (2) in /home/la02/16840949/admin/login.php on line 6

Is your code doing a session_save_path call? If not you need to check the value of the session.save_path variable in the applicable php.ini file. You can create a simple script in your document root with:

<?php phpinfo();

If you have any question about the runtime value of these settings.

This could also be a permissions issue depending on how your php/web server is configured. Usually the session files would be written to the /tmp directory, but in your case they are not. If you actually intended to write the session file to the place where it is being written, if that directory does not exist, and/or the user that is running the php process does not have permissions to read/write those files, then you're going to get the errors you're getting.

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

There is no header URL. Use Location: http://domain.com for redirecting.

Also you can't output anything before sending headers, so remove all echos before it.

In your first code you have to add die after sending header. Without it, script will be executed to the end (maybe not a problem here, but you should always stop script while redirecting).

Upvotes: 3

Related Questions