I'm a complete noob when it comes to sessions and cookies, and only started a couple of days ago using each of them:
I've run into a problem with the following script, it will register the sessions, but it won't set the cookies, and to be honest, i have no idea why :/
Code: Select all
///////////// First Page ///////////////////
<?php
session_start();
if ( isset( $HTTP_COOKIE_VARSї'username'] ) && isset( $HTTP_COOKIE_VARSї'password'] ))
{
if ( $HTTP_COOKIE_VARSї'username'] == 'jason' && $HTTP_COOKIE_VARSї'password'] == 'password')
{
header("Location: page5.php" );
}
else
{
header("Location: page3.php" );
}
}
else
{
echo 'You need to log in.<br /><form action="page4.php" method="post">
<input type="text" name="username" />
<input type="password" name="password" /><br />
<input type="checkbox" name"autologin" />
<input type="submit" />
</form>';
}
?>
/////////////////////////////////////////////
//////////// Second Page ///////////////////
<?php
session_start();
if ( $HTTP_POST_VARSї'username'] == 'jason' && $HTTP_POST_VARSї'password'] == 'password' && $HTTP_POST_VARSї'autologin'] )
{
setcookie("username", $HTTP_POST_VARSї'username'], time()+3600);
setcookie("password", $HTTP_POST_VARSї'password'], time()+3600);
header("Location: page5.php" );
}
elseif ( $HTTP_POST_VARSї'username'] == 'jason' && $HTTP_POST_VARSї'password'] == 'password' )
{
$HTTP_SESSION_VARSї'username'] = $HTTP_POST_VARSї'username'];
$HTTP_SESSION_VARSї'password'] = $HTTP_POST_VARSї'password'];
header("Location: page7.php" );
}
else
{
header("Location: page3.php" );
}
?>
///////////////////////////////////////////
///////////// Third Page /////////////////
<?php
session_start();
if ( $HTTP_COOKIE_VARSї'username'] == 'jason' && $HTTP_COOKIE_VARSї'password'] == 'password')
{
echo 'Congratulations, you have logged in! - cookies';
echo '<br><a href="page6.php">Log out</a>';
}
elseif ( $HTTP_SESSION_VARSї'username'] == 'jason' && $HTTP_SESSION_VARSї'password'] == 'password' )
{
echo 'Congratulations, you have logged in! - sessions';
echo '<br><a href="page6.php">Log out</a><br />';
}
else
{
echo 'Please Try Again: <a href="page3.php">Log in</a><br />';
}
echo 'm00';
?>
* i know of a security flaw in this, i'm just learning about using sessions and cookies :P *
//////////////////////////////////////////
///////////// Forth Page /////////////////
<?php
session_start();
if ( isset( $HTTP_COOKIE_VARS ))
{
setcookie("username", $HTTP_POST_VARSї'username'], time()-3600);
setcookie("password", $HTTP_POST_VARSї'password'], time()-3600);
}
session_destroy();
header("Location: page3.php" );
?>
/////////////////////////////////////////All the code from 4 pages is put into 1 here
If anyoen can help, would be much appreciated
ta