douzheng5717 2013-12-05 04:23
浏览 190

php设置cookie重定向

I need a domain example.com redirects to sub-domain a.example.com when I type on the address bar.

<script type="text/javascript">
$(function(){
    var city = readCookie('city');
    if(city==null && city==''){
    window.location.href = 'http://' + city + '.example.com';
}
$('#citygo').change(function(){
    var city = $(this).val();
    createCookie('city', city, 28);
    window.location.href = 'http://' + city + '.example.com';
});
});
</script>

<body>
<select id="citygo">
    <option value="0">Select City</option>
    <option value="amsterdam">Amsterdam</option>
    <option value="newyork">New York</option>
    <option value="london">London</option>
    <option value="cardiff">Cardiff</option>
    </select>
</body>

The cookie on the server side is not holding so the domain cannot remember sub-domain. What am i doing wrong? Any help will be very much appreciated.

<?php 
$hour = time() + 50400; 
setcookie(My_Site_Location, $_POST['citygo'], $hour); 

$Loc=$_COOKIE["city"]; 
if(isset($_POST['city'])) 
$Loc=$_POST['city']; 

if (empty($Loc)) { 
    header("Location: http://{$_COOKIE["city"]}.example.com");  
    } else { 
    header("Location: example.com/$Loc.php");  
} 
?>
  • 写回答

2条回答 默认 最新

  • dongzao3923 2013-12-05 04:41
    关注

    I have faced same issue but i solved my problem using the below code in PHP You are not setting the path and expiration on the cookie. If you have subdirectories it is normally good to set a master cookie that is the root path '/'.

    To read your cookie using PHP, try this...

    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    

    ...and search the array for your "city" cookie. Further docs on setting and reading cookies using entirely PHP, check here.

    You must call this function before any other code is echoed to the page. (before headers)

    This will help you

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装