Help with cookies problem please

Discussion in 'PHP' started by web-master, Jun 29, 2008.

  1. #1
    I have a file called 2.php and it should write a cookie and then file 3.php read it back.

    2.php contains
    3.php contains
    However when I have opened 2.php and then 3.php nothing happens! I looked at the error log and got the following
    Please help, I really need this to work,

    Cheers Web-Master
     
    web-master, Jun 29, 2008 IP
  2. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    your parameter deficient
    you must post a time
    example
    <?php
    setcookie("username", "john",time()+60*60);
    ?>

    time()+60*60 = 1 Hour
     
    xfreex, Jun 29, 2008 IP
  3. web-master

    web-master Peon

    Messages:
    838
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Still doesn't work! Also I was under the impression that you didnt have to have a time defined?
     
    web-master, Jun 29, 2008 IP
  4. Spawny

    Spawny Well-Known Member

    Messages:
    252
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #4
    i dont see nothing wrong with the code it should work
     
    Spawny, Jun 29, 2008 IP
  5. web-master

    web-master Peon

    Messages:
    838
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think the clue to the problem is in the error given, but I have no idea how to fix it.
     
    web-master, Jun 29, 2008 IP
  6. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you give a full code
    because error another codes
     
    xfreex, Jun 29, 2008 IP
  7. web-master

    web-master Peon

    Messages:
    838
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i have provided the full code above, thats all thats in the 2 files.
     
    web-master, Jun 29, 2008 IP
  8. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you may add a
    ob_start(); session_start(); => top
    ob_end_flush(); => bottom
     
    xfreex, Jun 29, 2008 IP
  9. web-master

    web-master Peon

    Messages:
    838
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Got the following when I did that

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/adscheme/public_html/h3/2.php:1) in /home/adscheme/public_html/h3/2.php on line 1

    Warning: Cannot modify header information - headers already sent by (output started at /home/adscheme/public_html/h3/2.php:1) in /home/adscheme/public_html/h3/2.php on line 2
     
    web-master, Jun 29, 2008 IP
  10. egoldseller

    egoldseller Guest

    Messages:
    213
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello !

    In 2.php put :

    
    <?php
    print "<script SRC=cookies.js LANGUAGE='JavaScript'></script><script>Set_Cookie( 'username', 'john', '1', '/', '', '' );</script>";
    ?>
    
    HTML:
    and In 3.php put this :

    
    <?php
    if(!empty($_COOKIE['username'])) {
    $username = $_COOKIE['username'];
    }else{
    // it's empty
    $username = " ";
    }
    echo "Username is : $username";
    ?>
    
    HTML:
    Add This JS file in the same Dir with 2.php & 3.php :

    cookies.js

    
    
    function Set_Cookie( name, value, expires, path, domain, secure ) 
    {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    
    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }
    function Get_Cookie( name ) {
    	
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
    }
    
    function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
    
    
    HTML:
    enjoy :D

    you can find more tutorials about Set_Cookie & cookies.js ...

    Any more Help , we will be happy to help you

    Best Regards
     
    egoldseller, Jun 29, 2008 IP
    web-master likes this.
  11. web-master

    web-master Peon

    Messages:
    838
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thanks thats perfect!
     
    web-master, Jun 29, 2008 IP
  12. egoldseller

    egoldseller Guest

    Messages:
    213
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hello ; web-master ;

    I'm Happy to help you ,

    if you need more help PM me ;

    Thank's

    Best Regards

    CHEMORUI.
     
    egoldseller, Jun 29, 2008 IP
  13. awev

    awev Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The error "Cannot modify header information - headers already sent by ..." usually means that you have something other than an end-of-file marker after the closing ">". Remove any spaces, new lines, etc.
     
    awev, Sep 14, 2008 IP
  14. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    That is an incredibly bad method of using cookies, there is so much work there that you don't need to do.

    The OP was doing it perfectly, but there may have been some whitespace (new lines, tabs) before the opening "<?php".

    When there is some text, anything at all, before the "<?php" the script will send out the http headers for the page immediately. When this happens, no cookies can be written. As far as i can remember, they can still be read.

    "web-master", check 2.php and make sure that the VERY first 5 bytes of the file are "<?php". Doing this should get the script working. If not, there are some issues with either your php settings, or your Apache settings (providing that you are using Apache).
     
    mNova, Sep 14, 2008 IP
  15. seo.king44

    seo.king44 Banned

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    the code seems fine to me 2
     
    seo.king44, Sep 14, 2008 IP