I have on my website:

index.php
members/login.php
members/redirect.php

What i am trying to do is i have login point to redirect passing the login info from login.php. in redirect.php I want it to set all of the session variables that i will need everywhere else in the site, however, using:

On members/redirect.php

session_start();
$_SESSION['id'] = 1;
header("location: ../index.php");

On index.php

session_start();
print $_SESSION['id'];

Output not a thing..
Any help is greatly appreciated.
Thanks
~Matt

Recommended Answers

All 5 Replies

The code is fine - at least it runs on my Apache installation, and the output is "1", as expected.

On Redirect.php

<?php
session_start();

//SQL INIT REMOVED

$sql = mysql_query("SELECT * FROM members WHERE username='$username' AND password='$password'"); 
$login_check = mysql_num_rows($sql);
	
if($login_check > 0){ 
    $row = mysql_fetch_row($sql);
    $_SESSION['id'] = $row[0];
}
?>

Later in that PHP scrip i am able to read the $_SESSION fin, but when i go to index.html:

<?php

session_start();

$testvar = $_SESSION['id'];
?>

<html>
<?php echo $testvar; ?>
</html

I still return nothing from that.. I know that i have all my names correct.. it just wont pass between pages. I have cookies enabled and it will now work on either of my computers.
Could this be because i am going from subdirectory to root? do i have to have it work out of all the same folder?
~Matt

Your HTML code is malformed, but I doubt that this is the cause of the unexpected behaviour.
If I understand the specs correctly, cookies are sent with the current document path as the cookie path. But all browsers which I have tested behave as expected on your code - the session cookie is set correctly and the session variables are available.
Did you try another browser?

i have tried it in both IE and Chrome and neither work =/
the html is different in the actual document i just made a quick mock up to show the usage.
i guess i will just stick to setting cookies instead of session vars.

Session variables rely on cookies - on the session cookie, to be precies, which in standard PHP has the name PHPSESSID. Check the cookies in your browser to see if this cookie is set, and if it has a path other than '/'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.