Problem with using cookies
For whatever reason, at least one beyond my experience, I cannot figure out why my cookies work in test trials but when I actually try to use them in the way that I have within my application, they do not work.
First the scenario and then show some code:
(from the way I am using cookies in my application)
A Web page, and at the bottom of it there is a small form which is nothing more than a few hidden fields and single button which when pushed, will send the user to another (processing) page.
The processing page/file does some processing one of which, checks to see if the user is logged in. If the user is not logged in, a cookie is created (holding the value zero) and the user is immediately sent right back to the page with the form button. From the user's point of view, they have never left the original web page (it all happens so quickly).
However, when checking to see if the cookie exists, there never seems to be one.
**** original web page with form button:
<form action="/directory/anotherdirectory/user_addfavs_tally.php" method="post">
<input type="hidden" name="theband" value="{ARTIST_ID}" />
<input type="hidden" name="thispage" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
<td align="right">
<table width="200" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="ADDtoFAVS" /></td>
</tr>
<tr>
<td align="center"><span class="style1"><?php @include("http://www.somedoamin.com/directory/anotherdirectory/user_spot_message.php"); ?></span></td>
</tr>
</table>
</td>
</form>
**** from processing page/file
if($_SESSION['visitors_username'] == '' || $_SESSION['someothervar'] == '') {
// If listener is not logged in, then do nothing but tell them they need to log in before adding favs
$user_spot = "0"; //message id
if ($user_spot) setcookie("thisdude", $user_spot, time()+30, "/directory/");
$gobackto = trim($_POST['thispage']);
header("Location: $gobackto");
exit;
}
**** from the include that you see on the original page
$message_to_show = $_COOKIE["thisdude"];
if ($message_to_show) {
switch ($message_to_show) {
case "0":
// User is not logged in. Show the following message:
echo "You must log in before you can add an artist to your favs.";
break;
case "1":
// Artist in question is already in User's top ten list
echo "This artist is already in your favs list.";
break;
case "2":
// There is no empty spot in User's top ten list
echo "Your top ten favs list is full.";
break;
default:
// default action is to show no message.
echo "did we get through the switch?";
break;
}
}
******* end code
Any help would be much appreciated as I have been working on this single, cookie, problem for roughly two days...
Thanks in advance.
|