PHP设置子域cookies和重定向

PHP设置子域cookies和重定向,php,redirect,cookies,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 =

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

$(函数(){
var city=readCookie(“城市”);
如果(城市!=null&&city!=''){
window.location.href='http://'+city+'.example.com';
}
$('#citygo')。更改(函数(){
var city=$(this.val();
window.location.href='http://'+city+'.example.com';
});
});
函数createCookie(名称、值、天数){
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
var expires=“;expires=“+date.togmString();
}
else var expires=“”;
document.cookie=name+“=”+value+expires+“path=/”;
}
函数readCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
现在,我需要在服务器端设置cookies以记住并重定向到已访问的子域。下面的代码不起作用,但应该是这样的。有人能告诉我如何设置cookie吗?任何帮助都将不胜感激

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

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


问题是您的
标题上缺少
关键字,[您试图使其行为类似于JS重定向]

谢谢Shankar。我只是尝试,但没有工作。我刚刚将脚本添加到index.php页面的顶部。我不确定这是否是共享主机的问题?
header("location:http://".$_COOKIE["city"].".example.com");