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 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Can you include $_POST variables in setcookie()?

    Hi all,

    On my comments page, currently I have sessions to remember the username and email. This is good and works well but not from once the user closes the window. I was thinking of using cookies to save it these pieces of data instead. So that it can be loaded each time (until the cookie expires).

    I know that you can use variables such as:

    PHP Code:
    <?php 

    $variable 
    "my cookie value - username";

    setcookie("user"$variabletime()+3600*24"/");

    ?>
    So that the cookie value is that of $variable and will expire in 24hours.

    Is it possible to save the $_POST variables of the username and email in the cookie? So far, everything (input form and then the comments) is on one page, or would it be doable if it were on two pages? So it'd be like:

    PHP Code:

    <?php 

    $usrnme 
    $_POST['username'];

    setcookie("user"$usrnmetime()+3600*24"/");
    Hope that makes sense,

    Thanks,
    Sam
    Last edited by helraizer; 11-19-2007 at 05:52 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sure. But you could have just tried it, ya know.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    What you have there:

    PHP Code:
    <?php 
    $usrnme 
    $_POST['username'];
    setcookie("user"$usrnmetime()+3600*24"/"); 
    ?>

    Should work perfectly!


    ~Jordan
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts
    It does not seem to actually set the cookie, for some reason. Any ideas?

    Also, doesn't the setcookie function have to go before the <html> tags? Which means that the cookie is sent before the $_POST variables are set, surely? Or have I got that part completely wrong?

    Sam

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    The post variables are set before your file is parsed.

  • #6
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Inigoesdr View Post
    The post variables are set before your file is parsed.
    In theory yes, but since I have everything on one page (including the form to be posted), until the form is submitted there are no values assigned to the variables. Is that where my problem lies, do you think?

    Sam

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,028
    Thanks
    2
    Thanked 315 Times in 307 Posts
    For your current code, check your web server log for errors and/or turn on full php error reporting (when learning, developing code, or debugging code, you should always use full php error reporting to get php to help you.) You will probably find that it is notifying you that the code is referencing undefined variables, because they have not been set yet. (Setting a cookie with an empty value actually deletes the cookie if I remember correctly.)

    Your code should only reference any $_POST variables after the form has been submitted, using conditional logic. If we assume that your form has a submit button with name="submit" -
    PHP Code:
    if(isset($_POST['submit']))
    {
    // code to process the form
    // only reference $_POST variables when the form has been submitted
    $usrnme $_POST['username'];
    ...

    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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