How to access session variables from a second php file?
As part of my attempt to create an ASP.net that has the same look and feel as an existing php application from another developer. (more about it can you read here: How to share sessions between PHP and ASP.net application?) I'm in the middle of the process of sharing userlogin state between my ASP.net and PHP application.
I have links like signin.aspx?foo=asdhhjkasd (ASP.net) and signin.php?foo=asdhhjkasd which tell the other application which user credentials should be used for authenticating a user.
Right now I'm stuck with PHPs session management: The existing php application consists of a index.php which includes several (some out of 100开发者_开发百科) other php files and performs its function. There is a sessionmanagement (session_start() involved et cetera.
What I want to do, is to call a page call signin.php with some parameters. Based on whether the used logged into the php application before or not, I simply want to redirect to the index.php, but I can't get a hold of the session variables.
How must my signin.php look like, to access the session variables used in the index.php. This is what I tried so far:
<?php
// session_start(); tried it with or without it
if($_SESSION['user_id'] != "")
{
header('Location:index.php');
}
else
{
echo "no redirect";
}
?>
I always get "no redirect" printed.
Or is my thinking wrong and it is not possible to access the session variables from another page in php when there is no post/get action involved?
Maybe I should say that my PHP abilities are a bit limited.
Make sure you have the correct cookie name (the default is PHPSESSID). You can change it with session_name()
.
Also: you should call session_start()
, so your code works even if session.auto_start is off.
Your script looks ok, I don't know why it would not work unless "session_start()" hasn't been initialized prior to using the sessions.
Also, where do you define the sessions? check that "session_start()" has been initialized there as well.
精彩评论