Results 1 to 8 of 8

Thread: How to create Cookies by using PHP???

  1. #1
    tlife143's Avatar
    tlife143 is offline x10Hosting Member
    Join Date
    May 2009
    Posts
    2

    Exclamation How to create Cookies by using PHP???

    I want a help on creation of cookies....

    How to create a cookies and how to access that cookie information???

    Plz help me!!!

    Wen i use setcookie(name,value,expiry);

    It's giving an error tht "Cannot modify header information - headers already sent by"
    Last edited by tlife143; 07-03-2009 at 12:40 PM.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,879

    Re: How to create Cookies by using PHP???

    Garrett Royce | Generally Helpful Member
    `ord(103)`@gjr.gr
    █ I promise not to try not to mess with your mind
    Tech Support | Programming Help | Flow Chart Tutorial

  3. #3
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: How to create Cookies by using PHP???

    simply put, setcookie() must be used before ANYTHING is output to the page.
    for example, the following will not work because there is a newline before the PHP engine is engaged:
    PHP Code:

    <?php
    setcookie
    (name,1,time()+3600);
    ?>
    The following will work however, despite it not being the first bit of code (because nothing else was output before it):
    PHP Code:
    <?php

    if (isset($_GET['name']))
    $name $_GET['name'];
    else 
    $name=0;

    if (isset(
    $_GET['age']))
    $age $_GET['age'];

    $calcage $age+5;

    if (
    $name)
    $output "$name, in 5 years you will be $calcage years old!";
    else
    $output 'please submit name & age';

    setcookie(name,1,time()+3600);
    echo 
    $output;
    ?>
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  4. #4
    Twinkie is offline Banned
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: How to create Cookies by using PHP???

    Modifying headers, such as locations and cookies, must be done before any output because the headers are sent as soon as you write to the page, as already said. That is why I find it helpful to put the body of php before everything, and use output control functions to write what has been written to after the body tag, shown below.
    PHP Code:
    <?php

    function ErrorHandler() {
         
    $flags "There has been an error!";
    }

    set_error_handler("ErrorHandler"E_ALL);

    ob_start();

    //PHP Code writes body.

    $html ob_get_clean();
    ob_end_clean();

    ?>
    <html><head><title>Bla</title></head><body>
    <?php

    if (isset($flags)) {
         echo 
    "<div style=\"color: red; height: 400px;\">" $flags "</div>";
    }

    echo 
    $html;

    ?>
    </body>
    </html>
    That way if there is an error anywhere on the page, you could better handle it. You could even redirect a user to an error page. Otherwise, you would have to be worried about where the error occurred.
    Last edited by Twinkie; 07-03-2009 at 05:13 PM.

  5. #5
    marshian's Avatar
    marshian is offline x10 Elder
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: How to create Cookies by using PHP???

    Quote Originally Posted by tlife143
    and how to access that cookie information?
    $_COOKIE["name"] contains the data in a cookie with the name "name". This information is not available when the cookie has just been set. In other words, the page has to be reloaded for $_COOKIE to be updated.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  6. #6
    drf1229 is offline x10Hosting Member
    Join Date
    Jun 2009
    Posts
    71

    Re: How to create Cookies by using PHP???

    Quote Originally Posted by Scoochi2 View Post
    simply put, setcookie() must be used before ANYTHING is output to the page.
    for example, the following will not work because there is a newline before the PHP engine is engaged:
    PHP Code:

    <?php
    setcookie
    (name,1,time()+3600);
    ?>
    The following will work however, despite it not being the first bit of code (because nothing else was output before it):
    PHP Code:
    <?php

    if (isset($_GET['name']))
    $name $_GET['name'];
    else 
    $name=0;

    if (isset(
    $_GET['age']))
    $age $_GET['age'];

    $calcage $age+5;

    if (
    $name)
    $output "$name, in 5 years you will be $calcage years old!";
    else
    $output 'please submit name & age';

    setcookie(name,1,time()+3600);
    echo 
    $output;
    ?>
    What he (or she) said

  7. #7
    daman371 is offline x10 Sophmore
    Join Date
    Nov 2006
    Location
    Louisiana
    Posts
    130

    Re: How to create Cookies by using PHP???

    Not true. setcookie usually has to be used elsewhere because. You do have to call ob_start(); before everything else on the page.

  8. #8
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: How to create Cookies by using PHP???

    Quote Originally Posted by daman371 View Post
    Not true. setcookie usually has to be used elsewhere because. You do have to call ob_start(); before everything else on the page.
    Completely not true. ob_start() will simply prevent anything being output, and instead saves everything until it is flushed.
    In my example I saved a string to a variable and then output that, basically performing the same function.

    You can get away with NEVER using ob_start, and still create cookies.
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. Places to learn php
    By JaWasabi in forum Scripts, 3rd Party Apps, and Programming
    Replies: 9
    Last Post: 01-13-2009, 02:03 AM
  4. php errors galore
    By DMG Online in forum Scripts, 3rd Party Apps, and Programming
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02: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
  •  
dedicated servers