PHP Session Is Empty On Callback Page
I have a duplicate of a live Wordpress site on my local using Xampp. My session path in php.ini is session.save_path = "C:\xampp\tmp", and the file setting these session variables are below. After I set the session and redirect the session is empty on the callback page. What am I missing? The folder is visible in Xammp for the tmp folder.
I also have my host file pointing the Domain Url to my localhost if that matters.
session_start();
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $requ开发者_JS百科est_token['oauth_token_secret'];
Also it isn't working on the live site.
Make sure that you're calling session_start()
before you attempt to use the session variables on your landing page. When you redirect, you cause the browser to send a new request for a new page. This new request doesn't yet have access to the session you were using in the initial request. Calling session_start()
recovers the session so you can use the data contained within it.
e.g.
FirstFile.php:
session_start();
$_SESSION['my_variable'] = "my data";
header("Location: http://example.com/SecondFile.php");
SecondFile.php:
session_start(); // **This line recovers the session**
echo $_SESSION['my_variable']; // This line will now print "my data"
You can avoid having to do session_start() in WordPress before having to read/write $_SESSION. Just do this in your plugin or theme's functions.php file:
if (!session_id()) {
add_action('init','session_start');
}
Note also that if you are using the following before you call a session_start(), it will work in many PHP applications, but will cause WordPress to ERASE your session variable for some odd reason:
session_set_cookie_params(0,'/');
So, I eliminated that statement and now use the trick in the functions.php, and now my plugins and themes support sessions just fine.
精彩评论