dongxian6715 2014-08-21 23:52
浏览 42
已采纳

Cookie不会存储表单值

I have 2 pages. One, index.php with the following code:

<?php  
if( isset( $_COOKIE['TzSelect']  ) ) 
{ 
     header( 'Location: http://xasxx.dasxxx.com/feed.php'); 
} 
?>
<html> ....
<form action='feed.php' method="post">
    <select name="myvalue" id="SelectTimeZone">
        <option value="Africa/Mbabane">Africa/Mbabane</option>
        <option value="Africa/Mogadishu">Africa/Mogadishu</option>
        <option value="Africa/Monrovia">Africa/Monrovia</option>
        </select>
<input type="submit">
</form>

And, the feed.php code:

if( isset( $_COOKIE['TzSelect']  ) ) 
{ 
     $myvalue = $_COOKIE["TzSelect"]; 
     date_default_timezone_set($myvalue);
} 
else 
{ 
     $number_of_days = 1000 ;
     $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ;
     setcookie( "TzSelect", myvalue, $date_of_expiry, "/" ) ;
} 

Now, when you run the index.php, you select a timezone from the drop down, and when you submit, the feed.php loads with the date_default_timezone_set utilizing the selected value and doing further calculations. It's also set, much lower down the actual file (which does not have any effect on the cookie at all) to show EST as the standard time zone, so all calculations would be done in EST if something doesn't work right. Also, this is a first-time only choosing the time zone (which is why I am storing it in a cookie). So, what I have is on the index.php page the very first thing it does is checks if the cookie exists. If it does, it redirects to the feed.php page where the calculations are done based on the already preset value stored in the cookie. Now, the cookie is definitely created, one because if I open the index.php page it redirects me to the feed where it does its calcs, and two because I can see the cookie details in the settings in Chrome:

Name:   TzSelect
Content:    myvalue
Domain: xasdas.sxasmze.com
Path:   /
Send for:   Any kind of connection
Accessible to script:   Yes
Created:    Thursday, August 21, 2014 7:20:12 PM
Expires:    Wednesday, May 17, 2017 7:20:12 PM

BUT, my problem is, the cookie doesn't have the selected value from the drop down list, in which case, the feed.php page loads, but instead of doing calculations with the selected option it does default EST calculations. I've been able to successfully pass the chosen value from the form to the feed page and do the proper calculations, but whenever I try to store that information in a cookie so the user doesn't have to set the timezone every time, it fails. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongshimao7115 2014-08-22 00:14
    关注

    You need to process your post, so:

    setcookie( "TzSelect", myvalue, $date_of_expiry, "/" ) ;
    

    Should be something like:

    setcookie( "TzSelect", $_POST['myvalue'], $date_of_expiry, "/" ) ;
    

    However, if you are building a list on the first page already, it would be a good idea to check $_POST['myvalue'] against the list of possible values to make sure you only set cookies with values that you expect. You should never trust user input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义