Session Variable does not work Wordpress

Asked

Viewed 329 times

1

I cannot recover session variable using the following code

This is a file to which requests are sent, I declare a session variable to use on another page that is called after completion of the file.

<?php
    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once( $parse_uri[0] . 'wp-load.php' );
    $_SESSION["enviou"] = "true";
?>

On this other page, I try to recover in the simplest way possible

if ($_SESSION["enviou"] == "" || $_SESSION["enviou"] == null){
     header("location:http://www.google.com");   
}else{
    $_SESSION["enviou"] == null;
}

But the variable is always coming empty, even making the declaration of the same in the previous file.

When entering the code session_start(); receive the following errors:

Warning: session_start() [function.session-start]: open(/var/lib/php-cgi/session/sess_6aqo4l2r50v2hguu8pnrqusvr7, O_RDWR) failed: No such file or directory (2) in /home/storage/a/6a/25/..../public_html/vl/wp-content/themes/....../page-formbarraresposta.php on line 2
Warning: Unknown: open(/var/lib/php-cgi/session/sess_6aqo4l2r50v2hguu8pnrqusvr7, 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 (/var/lib/php-cgi/session) in Unknown on line 0

Could this be a server error? Since this same code has already been used in another project and worked perfectly.

  • To start a Session, we use the session_start() function. For a good functioning, it cannot be after any data OUTPUT (echo, print, HTML codes, etc.). It is recommended to be in the first line of the code.

1 answer

1

The error is very explicit in what it says, the path where PHP saves sessions did not exist on my server, so I found the code

session_save_path(realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../tmp'));
session_start();

And then it worked perfectly.

  • Thanks, beautiful solution... I had to use this in a very old version of PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.