Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Cookie and redirect 2

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
I wonder if this question has been done to death, but I'm trying to do something whihch I think is really simple.

I want to display two images on a page, have two radio buttons with a submit button, once one of the options is selected and the submit button is click, I want to set a cookie with the value of the radio object (option 1 or option 2) and then send the user to another page confirming their choice.
If the user then tries to go to the choice page again, if the cookie is set, redirect them to the results page.

Here's my code so far, which does the majority of the functionality described, but I need the redirect if the cookie is set.

Code:
<?php
$vote =  $_POST['vote'];
$self =  $_SERVER['PHP_SELF'];

if ($vote != null) {
	setcookie( "userVote", $vote , time() + 604800 );
	header( "Location:design_ok.php" );
	exit();
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>New Website Design</title>
</head>

<body>

<span style="font-size:24px;">What's you choice?</span><br />

<table width="100%" border="1" cellpadding="3">
  <tr>
    <td align="center" bgcolor="#FFFFFF"><span class="style1">Option 1 </span></td>
    <td align="center" bgcolor="#FFFFFF"><span class="style1">Option 2 </span></td>
  </tr>
  <tr>
    <td align="center"><img src="design1_web.jpg" width="585" height="438" alt="Design 1" title="Design Option 1" /></td>
    <td align="center"><img src="design2_web.jpg" width="582" height="437" alt="Design 2" /></td>
  </tr>
  
  <form id="voteForm" name="voteForm" action="<?php echo( $self ); ?>" method = "post">
  <tr>
    <td align="center" colspan="2">
		<input type = "radio" name = "vote" value = "Option 1">Option 1
		<input type = "radio" name = "vote" value = "Option 2">Option 2
		<br />
		<input type="submit" name="Submit" value=" Vote " />
    </td>
	</tr>
	</form>
	
</table>
</body>
</html>

Firstly, is the best way to do it and secondly, I want the redirect if cookie is set.

Many thanks ...
 
This is untested, but I think its something like this

Code:
<?php
$vote =  $_POST['vote'];
$self =  $_SERVER['PHP_SELF'];
if (isset($_COOKIE['userVote'])){
    header( "Location:design_ok.php" );
    exit();
}
if ($vote != null) {
    setcookie( "userVote", $vote , time() + 604800 );
    header( "Location:design_ok.php" );
    exit();
    }
?>

-----------------------------------------
I cannot be bought. Find leasing information at
 
That works a treat in terms of the redirect.
I obviously understand that if someone deletes the cookie, they can vote again, but it's not that important really.

A subsequent question is how do I get some text displayed on the resultant page (design_ok.php) to show the user has already voted?

Here's the snippet of code from that script:
Code:
<?php
if(isset($_COOKIE['lastVisit']))
	$visit = $_COOKIE['lastVisit']; 
else
	echo "You have already voted!";

echo "Your last visit was - ". $visit;
echo "<br />You voted for ". $votes;
?>

But I can't get the "you have already voted" text to be displayed. Any ideas?
 
Well I don't know what lastVisit is. I would think you'd want something like
Code:
if (isset($_COOKIE['userVote'])){
$vote = $_COOKIE['userVote'];
echo "You have already voted!<br/>";
echo "You voted for " . $vote . "<br/>";
}
//and maybe
if (isset($_COOKIE['lastVisit'])){
echo "Your last visit was - " . $_COOKIE['lastVisit'];
}

-----------------------------------------
I cannot be bought. Find leasing information at
 
cookies are not a great place to keep server data. is there any reason why you don't use a session store instead? and then just rely on the session cookie?
 
Thanks for the response. I'll give this a try when I'm next at work (maybe tomorrow!).

jpadie - I just wanted something really simple so I decided to do it this way. Its a local thing within the office (albeit its on a public facing server).
You may argue that session cookies are just as easy, but I couldn't even complete this one!

Thanks again ...
 
d0nny

sessions are probably simpler in the long run. promise!

consider this

Code:
session_start();
$_SESSION['something'] = 'somevalue';

Code:
session_start();
if (isset($_SESSION['something']){
 echo "Session is set. Session value for item 'something' is $_SESSION[somevalue]";
} else {
 echo "Session not set";
}

with sessions you can reliably persist data across page visits. there is little limit to the information you can store in a session either, whereas there are quite stringent limits on cookie data, not to mention that it will involve more bandwidth.


 
Thanks.
I know session cookies are much better and in fact I developed a login system for my web pages using a session cookie. But I am really a beginner when it comes to sessions.

Is there a good tutorial on sessions I could read with good examples?
 
I wrote one a few years ago and posted it on to a site that's now dead (not mine). i will see whether i can dig it out and post it at rathercurious.net. I can't vouch that it's good though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top