简体   繁体   English

PHP设置子域Cookie并重定向

[英]PHP set subdomain cookies and redirect

I have the cookies and subdomain selection for header: 我有标题的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>

Now I need to work on the server side to set cookies to remember and redirect to a visited subdomain. 现在,我需要在服务器端进行工作以设置cookie以便记住并重定向到访问的子域。 The code below is not working but should be something like that. 下面的代码不起作用,但是应该是这样的。 Would someone show me how to set cookie? 有人会告诉我如何设置Cookie吗? Any help will be very much appreciated. 任何帮助将不胜感激。

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

Replace your 更换您的

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

to

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

Problem was you were missing location keyword on your header , [You were trying to make it behave like a JS Redirect] 问题是您在header缺少location关键字, [您正试图使其表现得像JS重定向]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM