www.webdeveloper.com
Results 1 to 13 of 13

Thread: php set subdomain cookies and redirect

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    12

    php set subdomain cookies and redirect

    I have the cookies and subdomain selection for header:

    Code:
    <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.

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

  2. #2
    Join Date
    Nov 2013
    Posts
    33
    try
    PHP Code:
    if (isset($_COOKIE["city"])) { 
        if (
    $_COOKIE["city"] == 'city') {
            
    header("Location: http://{$_COOKIE["city"]}.example.com");
        } 


  3. #3
    Join Date
    Nov 2013
    Posts
    33
    also change this
    Code:
    $('#citygo').change(function(){
    	var city = $(this).val();
    	window.location.href = 'http://' + city + '.example.com';
    });
    to - which will set your cookie
    Code:
    $('#citygo').change(function(){
    	var city = $(this).val();
    	createCookie('city', city, 28);				
    	window.location.href = 'http://' + city + '.example.com';
    });

  4. #4
    Join Date
    Dec 2013
    Posts
    12
    Thanks unasAquila. It is working partially. The domain can redirect to the subdomain but keeps running repeatedly. How to stop the loop?

  5. #5
    Join Date
    Nov 2013
    Posts
    33
    are you calling the same script on each page?

  6. #6
    Join Date
    Dec 2013
    Posts
    12
    I added the script to the top of index.php page.

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

  7. #7
    Join Date
    Dec 2013
    Posts
    12
    That's the script I use on the top. I only follow your instruction.

  8. #8
    Join Date
    Nov 2013
    Posts
    33
    sorry i should have noticed the conditional was wrong change
    PHP Code:
    if(city!=null && city!=''){
        
    window.location.href 'http://' city '.wellbeing.uk';

    to

    PHP Code:
    if(city==null && city==''){
        
    window.location.href 'http://' city '.wellbeing.uk';


  9. #9
    Join Date
    Dec 2013
    Posts
    12
    The loop stops but the cookie is not holding and the domain (example.com) cannot redirect to subdomain (a.example.com) when I type on the address bar. It was working partially with the previous code.

  10. #10
    Join Date
    Dec 2013
    Posts
    12
    I am trying to approach it from the other direction but still not good.

    Code:
    <?php 
    $hour = time() + 50400;  
    setcookie(My_Site_Location, $_POST['citygo'], $hour); 
    
    //Cookie
    $Loc=$_COOKIE["city"]; 
    if(isset($_POST['city'])) 
    $Loc=$_POST['city']; 
    
    if (empty($Loc)) { 
        header("Location: http://{$_COOKIE["city"]}.example.com");  
        } else { 
        header("Location: example.com/$Loc.php");  
    }

  11. #11
    Join Date
    Dec 2013
    Posts
    12
    How about this code? Can you fix it?

    Code:
    <?php 
    $hour = time() + 50400;  //Time you want the cookie to last, currently 14 hours 
    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);
           }
       }
    }
    ?>

  12. #12
    Join Date
    Nov 2013
    Posts
    33
    The Javascript version works best and as long as you have the code on every page you should not have any problems.

    as to the php
    PHP Code:
    self::set_cookie 
    will only work in a class context.

    are you wanting it to be all php or is all javascript ok?

  13. #13
    Join Date
    Dec 2013
    Posts
    12
    javascript and PHP are two completely different animals. For now I prefer to run on the server side but I am not familiar with it. I just need a simple script that work so can you please help? Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles