PHP设置子域cookie和重定向

时间:2013-12-04 19:17:11

标签: php redirect cookies

我有标题的Cookie和子域名选择:

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<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();
window.location.href = 'http://' + city + '.example.com';
});
});

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>


<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>

现在我需要在服务器端工作以设置要记住的cookie并重定向到已访问的子域。下面的代码不起作用,但应该是这样的。有人会告诉我如何设置cookie吗?任何帮助将非常感谢。

<?php 
if (isset($_COOKIE["city"])) { 
if ($_COOKIE["city"] == 'city') { 
header("window.location.href = 'http://' + city + '.example.com'"); 
} 
} 
?>

1 个答案:

答案 0 :(得分:0)

替换

header("window.location.href = 'http://' + city + '.example.com'"); 

header("location:http://".$_COOKIE["city"].".example.com"); 

问题是您在 location 上错过了 header 关键字, [您试图使其行为像JS一样重定向]