Cookies Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YCrowley
Forum Newbie
Posts: 1
Joined: Thu Mar 06, 2003 4:32 pm

Cookies Problem

Post by YCrowley »

I'm learning how to use sessions and cookies together so that i'm able to build an authencation system for a site.
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&#1111;'username'] ) && isset( $HTTP_COOKIE_VARS&#1111;'password'] ))
&#123;
if ( $HTTP_COOKIE_VARS&#1111;'username'] == 'jason' && $HTTP_COOKIE_VARS&#1111;'password'] == 'password')
&#123;
header("Location: page5.php" );
&#125;
else
&#123;
header("Location: page3.php" );
&#125;
&#125;
else
&#123;
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>'; 
&#125;
?> 
 
/////////////////////////////////////////////
 
//////////// Second Page ///////////////////
<?php
session_start();
if ( $HTTP_POST_VARS&#1111;'username'] == 'jason' && $HTTP_POST_VARS&#1111;'password'] == 'password' && $HTTP_POST_VARS&#1111;'autologin'] ) 
&#123;
setcookie("username", $HTTP_POST_VARS&#1111;'username'], time()+3600);
setcookie("password", $HTTP_POST_VARS&#1111;'password'], time()+3600);
header("Location: page5.php" ); 
&#125;
elseif ( $HTTP_POST_VARS&#1111;'username'] == 'jason' && $HTTP_POST_VARS&#1111;'password'] == 'password' )
&#123;
$HTTP_SESSION_VARS&#1111;'username'] = $HTTP_POST_VARS&#1111;'username'];
$HTTP_SESSION_VARS&#1111;'password'] = $HTTP_POST_VARS&#1111;'password'];
header("Location: page7.php" );
&#125;
else
&#123;
header("Location: page3.php" ); 
&#125; 
?> 
///////////////////////////////////////////
///////////// Third Page /////////////////
<?php 
session_start();
if ( $HTTP_COOKIE_VARS&#1111;'username'] == 'jason' && $HTTP_COOKIE_VARS&#1111;'password'] == 'password')
&#123;
echo 'Congratulations, you have logged in! - cookies'; 
echo '<br><a href="page6.php">Log out</a>';
&#125;
elseif ( $HTTP_SESSION_VARS&#1111;'username'] == 'jason' && $HTTP_SESSION_VARS&#1111;'password'] == 'password' )
&#123;
echo 'Congratulations, you have logged in! - sessions'; 
echo '<br><a href="page6.php">Log out</a><br />';
&#125;
else
&#123;
echo 'Please Try Again: <a href="page3.php">Log in</a><br />';
&#125;
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 ))
&#123;
setcookie("username", $HTTP_POST_VARS&#1111;'username'], time()-3600);
setcookie("password", $HTTP_POST_VARS&#1111;'password'], time()-3600);
&#125;
session_destroy();
header("Location: page3.php" ); 
?> 
/////////////////////////////////////////

All the code from 4 pages is put into 1 here 8)

If anyoen can help, would be much appreciated

ta :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Sorry don't have time (or probably the ability) to look through all the code but here's a quick and kind of obvious tip to help with debugging cookie scripts: look in the C:/windows/cookies folder to see if your cookie has been set or not.
Post Reply