简体   繁体   中英

New to PHP and having problems with cookies

Im new to PHP, and by that I mean BRAND NEW. Today is the first time I've really even sat down and gave it a shot for an extensive period of time.... And the thing I'm trying to achieve is kinda silly... ive been making a little flash game website for quite some time now... Just to get my hands dirty with web design and HTML and CSS i seem to be doing fine with. The website is just a simple flash game website that I made so I could play some of my favorite flash games at school when i had nothing better to do. and I had remembered on an old website i use to go to in order to do the same thing had a "panic button" underneath every game which when clicked just took you to google... I thought it was a funny and smart idea so i wanted to improve on it, i figured i would make a little PHP script that would enable the user to change the link to whatever you want. So lets say the teacher says you need to be on, oh i dont know... SCIENCE.COM! you just copy and paste the URL into the input bar on the front page and it automatically saves it in your cookies that thats the URL you want to go to when you click the "panic" button... I guess ill post the code ive been trying to get it to work with so you can all see.

Heres the PHP:

$expire=time()+0*0*12*0;

setcookie('panic', $panic, $expire);

?>

Here is the HTML:

<form method="post">
<input type="text" name="panic" size="80" id="panic"/>
<input type="submit" value="Submit"/>
</form>
<br />
<?php
echo ('click <a href="\'$_COOKIE {\'$panic\'}\'">HERE</a> to see if it worked!');
?>

I probably dont seem too smart from this... Ive literally been going at this for hours, for some reason i just cant seem to figure it out.... And i dont know if its possible, can i have the main PHP for the cookie in a separate file from the HTML or no?...

I must also point out that out of desperation i went around editing lots of names, so i must say if anything seems extremely out of place its more then likely because i was getting frustrated and messing the the code in odd ways.

It's because your html is wrong. You can't set cookies that way. PHP is run server-side, not client-side. Try this.

HTML

<form method="post">
<input type="text" name="panic" size="80" id="panic"/>
<input type="submit" value="Submit"/>
</form>
<br />
<?php
echo ('click <a href="?viewcookie=1">HERE</a> to see if it worked!');
?>

PHP

<?php
error_reporting(E_ALL ^ E_NOTICE); // prevent it from erroring if something isn't defined
if($_GET["viewcookie"]){
    echo $_COOKIE["panic"];
} else if($_POST){
    $panic = $_POST["panic"]
    setCookie('panic', $panic, time()+(60*12)); //expires in 12 minutes
    echo $_COOKIE["panic"];
}
?>

I think your problem is due to misused quotes around your PHP in the following code. Also you are using curly braces for your $_COOKIE[] selector, and trying to select the cookie by its contents, rather than its name (you are using $panic instead of 'panic' ):

<?php echo ('click <a href="\\'$_COOKIE {\\'$panic\\'}\\'">HERE</a> to see if it worked!'); ?>

Single quotes mean that you don't need to escape double quotes, and also that any PHP variables you include inside the quotes won't automatically be replaced, so these will need to be concatenated outside of the quotes.

Try replacing it for this:

<?php echo ('click <a href="'.$_COOKIE['panic'].'">HERE</a> to see if it worked!'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM