简体   繁体   中英

PHP sessions not working as expected

So I am creating an application (new -ish to sessions) and have been trying to create a simple error handling statement using sessions.

To simplify things, let me describe the basics.

  • User enters query on page 1
  • User presses submit
  • Page 2 is loaded to check the value of the query
  • If the query is set, it is run and the result is displayed on page 3
  • If it is empty however, an error is caught, set and the user is redirected back to page 1

What I care about is the last step as I cannot get it to work for some reason.

Here is the code pertaining to the error:

Page 1s relevant code:

<?php
session_start();

$ERROR = $_SESSION['error'];

if($ERROR) {
    echo $ERROR;
}

?>

And on page 2:

<?

session_start();

---------------- And as we go down the file a bit ----------------

if(trim($getQuery == "")){
    $ERROR = "no search criteria entered";

    $_SESSION['error'] = $ERROR;

    if(!isset($_SESSION['error'])) {
        die("the session error was not set for some reason");
    }

    $url = "localhost:8000/mysite"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case

    header("Location:" . $url);
}
?>

$getQuery is the value captured in the query box on page 1 and sent via the post method to page 2 as you may assume naturally.

But when I enter nothing in the query box and then send the query, the page refreshes (as it should when page 2 realises that the query is empty and header location reloads the page) but no error is shown, which it should considering I check on page 2 that it is set.

Any ideas?

Cheers, -- SD

You have a typo in if(trim($getQuery == "")) { ... } it should be if(trim($getQuery) == "") { ... } , since you only want to trim the $getQuery variable, and not the whole condition. If you change this, then it will work.

Here's a minimum working example

<?php // 1.php
session_start();
$ERROR = $_SESSION['error'];
if($ERROR) {
    echo $ERROR;
}
?>

<?php // 2.php
$getQuery = ""; // This is empty so it will redirect to 1 and show error message
session_start();
if(trim($getQuery) == ""){
    $ERROR = "no search criteria entered";

    $_SESSION['error'] = $ERROR;

    if(!isset($_SESSION['error'])) {
        die("the session error was not set for some reason");
    }
    $url = "1.php"; //index.php is page 1 in this case so I just redirect to the parent directory as index is loaded by default obviously in that case
    header("Location:" . $url);
}
?>

Sometimes the browser redirect (your header("Location")) can be quicker than the server.

Just before the redirect you should put

session_write_close()

Just to make sure the session is written for next time.

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