开发者

Sessions not working PHP

My website doesn't start a session when I visit, I don't know why but my website works like this:

<?php

session_start(); 

$title = "Home"; 

include("include/header.php");
include("include/functions.php"); 

?>

...HTML stuff here...

<?php 

include("footer.php"); 

?>

But when I check with Cookies (add-on for Firefox) there are no sessions started... I used session_regenerate_id(); but it doesn't work at all.

It fails to log in since there are no sessions, I do not have any session_destroy() in my w开发者_如何学Goebsite, only in the logout.

But funny thing is, when I login (without refreshing or navigating just yet) and then click on the logout button, there is a session on my website, then when I log in again, it tells me that I am logged in BUT if I login and navigate or refresh, it doesn't tell me that I'm logged in since there are no sessions...

Logout:

<?php

session_start();
session_destroy();

setcookie("cookie-name", "", time()-60, "", "", 0);

header("Location: ../index.php");

exit;

?>

What do I do?


You must have session_start() at the beginning of every file that is being accessed and uses sessions. The name is misleading, session_start() actually doesn't start a new session but initialzes PHP session menagment.


Not sure if it's related, but there was a strange PHP quirk that required the SESSION_START() to be on the line immediately below the <?php tag. Something about whitespace and extra things above the session used to make it go haywire for me. I've been using Zend of late, which avoids that issue with its own session handling system.

You might try doing a print_r($_SESSION) to see if there's anything in the session array at all.


It's probably because you are not setting a session in either of the examples you have given, you have to have a line like the one below to actually create a session, and then to access the session variables on all subsequent pages you need session_start();

$_SESSION['example'] = 'something';


It doesn't look like your setting anything in the session or the cookie.

If you want to pass information around in the session you'll need to assign the necessary values in the $_SESSION variable.

For example on your main page you can do:

<?php
session_start();
$_SESSION['myVariable'] = "my text";
?>

And then on any subsequent pages you can access the variable you've set.

<?php
session_start();
echo $_SESSION['myVariable'];  //This will print "my text"
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜