homepage Welcome to WebmasterWorld Guest from 54.158.186.118
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php set subdomain cookies and redirect
iseven



 
Msg#: 4627870 posted 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'");
}
}
?>

 

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4627870 posted 8:57 am on Dec 5, 2013 (gmt 0)

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");...]
}

swa66

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



 
Msg#: 4627870 posted 9:05 am on Dec 5, 2013 (gmt 0)

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.

swa66

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



 
Msg#: 4627870 posted 9:05 am on Dec 5, 2013 (gmt 0)

dupe - sorry

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4627870 posted 9:08 am on Dec 5, 2013 (gmt 0)

What swa66 said.
BTW: Welcome to WebmasterWorld!

iseven



 
Msg#: 4627870 posted 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);
}
}
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved