tags:

views:

58

answers:

4

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 website, 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?

+1  A: 

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.

nikic
index.php, login.php, register.php has session_start() at the very beginning before it includes the header.php and other files.
YouBook
A: 
bpeterson76
I've tried that, also var_dump($_SESSION) but it appears there is nothing.
YouBook
+1  A: 

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';
Catfish
When I want to log in, it writes the session: $_SESSION['USER'] = $row['username']; but in the output it doesn't work, the session just won't work at all
YouBook
Have you checked to ensure that $row['username'] contains a value?
Catfish
+1  A: 

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"
?>
brainimus
I know how to do sessions, if I write the sessions after the session_start(), it doesn't work
YouBook