Handling Cookies

This was suppose to be a simple assignment and I kind of turned it into something more ::). This is suppose to demonstrate the create and expire of a cookie; it does, but with one bug that I can not figure out. It uses a form to collect user data and save it to cookies if the user selects to do so. The form OR the cookies are used to format the users input.

Everything works as intended, except when you go through a second iteration of using cookies. For some reason it ignores my variable to output the “cookie has expired” and continues a cookie create and expire loop throughout the refreshes. of course, this isn’t intended, it’s suppose to stop when the cookie expires and provide a link to reset the session and start from scratch again once clicked … as you’ll see in the first iteration.

The link to the actual page is in the first line of the PHP code section below, since it wouldn’t let me post it here …

[php]http://rvcphp.net16.net/week5/week5.php

<?php $action = @$HTTP_GET_VARS['action']; if( $action == 'dump' ) { session_start(); // Unset all of the session variables. $_SESSION = array(); // If it's desired to kill the session, also delete the session cookie. // Note: This will destroy the session, and not just the session data! if ( ini_get( "session.use_cookies" )) { $params = session_get_cookie_params(); setcookie( session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } // Finally, destroy the session. session_destroy(); } ?> ... Some HTML to include and process the form ... <?php session_start(); function userText() { echo "

". $_SESSION['userText'] ."

"; } if( isset( $_POST['submit'] )) { $_SESSION['font'] = $_POST['font']; $_SESSION['fontSize'] = $_POST['fontSize']; $_SESSION['fontColor'] = $_POST['fontColor']; $_SESSION['userText'] = $_POST['userText']; if( @$_POST['useCookie'] == 'yes' ) { if( isset( $_COOKIE['font'] )) { $_SESSION['font'] = $_COOKIE['font']; $_SESSION['fontSize'] = $_COOKIE['fontSize']; $_SESSION['fontColor'] = $_COOKIE['fontColor']; $_SESSION['expire'] = "yes"; echo "
Preferences saved, cookies active for 30 seconds.
"; echo "Refresh after 30 seconds to obsurve cookie death.
"; echo "Font - ". $_SESSION['font'] ."
"; echo "Font Size - ". $_SESSION['fontSize'] ."
"; echo "Font Color - ". $_SESSION['fontColor']; userText(); } elseif( isset( $_SESSION['expire'] )) echo "
Cookies have expired, please click HERE to start a new session."; else { setcookie( 'font', $_POST['font'], time()+30 ); setcookie( 'fontSize', $_POST['fontSize'], time()+30 ); setcookie( 'fontColor', $_POST['fontColor'], time()+30 ); echo "
Cookies created, please refresh to display formated text with cookie data.
"; } } else { echo "
Preferences not saved, cookies not active."; userText(); } } ?>[/php]

It just baffles me why it’s ignoring just that one variable on the second time around ???

So, no ideas? Has anyone even attempted to debug?

No ideas?

Sponsor our Newsletter | Privacy Policy | Terms of Service