duancaoqin6683
2014-02-17 01:49
浏览 20
已采纳

PHP cookie设置

I hate to say it but I have been working on what should have been a 30 minute assignment for a good 6 hours now with little to no progress. I am attempting to capture a name and email in a form, and set them to cookies that will last 10 minutes. While the cookies are active, the page should skip the form and just display the input. I have tried this with both cookies and sessions and cannot get it to work. At this point I have written and deleted at least a hundred lines of code and just can't really see what the problem is. This is my first time working with PHP. Any help would be appreciated. Currently this code creates the form, takes the info and posts it to the page correctly. When I go back to the page, it shows the form again. I assume this means the cookie isn't setting / sticking.

<?php 
 if (!empty($_POST)) {
  setcookie('Cname',$_POST['name'], time()+600);
  setcookie('Cemail', $_POST['email'], time()+600);
//  header("Location:HW2.php");
 } 

?>
<html>
<head>
<title> Assignment 2 Alcausin </title>
</head>
<body>

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$visibleForm = True;
if(isset($_COOKIE['name'])){
    $visibleForm = False;
}
if(isset($_POST['submit'])){
    $visibleForm = False;
    echo "Your Name: ";
    echo $_COOKIE['Cname'];
    echo "<br>";
    echo "Your Email: ";
    echo $_COOKIE['Cemail'];
}
if($visibleForm){ // close php if form is displayed
    ?>      
<form action ="HW2.php" method="post">
    Name:<font color = red>*</font> <input type="text" name="name"><br>
    E-mail:<font color = red>*</font> <input type="text" name="email"><br>
    <input type="submit" name="submit" value="Submit">
</form>
<?php   // back to php
}
?>

</body>
</html>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题