Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php set subdomain cookies and redirect

   
7:22 pm on Dec 4, 2013 (gmt 0)



I have the cookies and subdomain selection for header:

<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. The code below is not working but should be something like that. Would someone show me how to set 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'");
}
}
?>
8:57 am on Dec 5, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



perhaps something like this:

if ( isset($_COOKIE["city"]) )
{
if ($_COOKIE["city"] == 'city')
{
header("Location: [city.example.com");...]
}

// or more general
header("Location: [".$_COOKIE["city"].".example.com");...]
}
9:05 am on Dec 5, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



First off javascript and PHP are two completely different beasts.

Javascript runs in the browser on the client
PHP runs on the server before any data is given to the client.

They both are independent languages
Javascript uses "+" to concatenate strings.
PHP uses "." to concatenate strings.
variables in PHP are written as $foo (note the dollar sign).

Javascript has access to the DOM, PHP has access to server resources, ...

Essentially you need to learn PHP just like you learned javascript - and keep in mind you can't just take javascript code and hope PHP would understand what you meant to do.
9:05 am on Dec 5, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



dupe - sorry
9:08 am on Dec 5, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



What swa66 said.
BTW: Welcome to WebmasterWorld!
5:12 pm on Dec 5, 2013 (gmt 0)



How about this code? Can you guys fix it? Thanks.

<?php 
$hour = time() + 50400;
setcookie(My_Site_Location, $_SERVER['citygo'], $hour, '/', 'example.com');

if (isset($_SERVER['citygo'])) {
$cookies = explode(';', $_SERVER['citygo']);
foreach ($cookies as $cookie) {
list($cookie_id, $cookie_value) = explode('=', $cookie);
if($cookie_id === $name){
self::set_cookie($cookie_id, $value, $expiry, $path, $domain);
}
}
}
?>