Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1

Thread: Cookie Fix

  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Cookie Fix

    Having trouble setting cookies? Try setting the globally, or alternately again, by headers.

    Had to use this on a clunky host. I hope people don't have this problem, but if you do, here is a fix. Note, if it fails to set the cookie globally, it will try by headers, which is not possible unless the function is called before any output.

    PHP Code:
    <?php

    function setaltcookie$name$value$expires ) {

        
    // Use up to two digits to define a expiration in days.
        
    $expires = ( ( strlen$expires ) <= ) ? ( time() + ( 86400 $expires ) ) : ( time() + $expires ) );


        
    $global = @setcookie$name$value$expires'/''/' );
        
        if ( ! ( 
    $global ) ) {

            
    // If we can't set the cookie globally, try setting a cookie by headers. 
            
    $header = @header('Set-Cookie: ' $name '=' $value '; expires=' $expires '; path=/; domain=' $_SERVER['HTTP_HOST'] );
            
            return 
    $header;
        
        }
        
        return 
    $global

    }

    if ( 
    setaltcookie'demo-cookie'md5microtimetrue ) ), ) ) {

        echo 
    'The cookie should now be set.';

    } else {

        echo 
    'Hmm, nope.';

    }

    ?>
    Last edited by Element; 04-25-2008 at 09:33 PM.


 

Posting Permissions

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