SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Help in PHP!!

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    sn00zeville
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Help in PHP!!

    I jez want to know what this means... I'm building a shopping cart with a registration form in it... It's working though, but what's up with those warnings?!..... HELP!!

    Warning: Cannot send session cookie - headers already sent by (output started at /home/virtual/site68/fst/var/www/html/register.php:2) in /home/virtual/site68/fst/var/www/html/register.php on line 4

    Warning: Cannot send session cache limiter - headers already sent (output started at /home/virtual/site68/fst/var/www/html/register.php:2) in /home/virtual/site68/fst/var/www/html/register.php on line 4
    Ok!
    Warning: Cannot add header information - headers already sent by (output started at /home/virtual/site68/fst/var/www/html/register.php:2) in /home/virtual/site68/fst/var/www/html/register.php on line 59

    Thanks! You have been registered.


    Here's my register.php code...

    <?php
    session_start();


    $hostname_mysql = "MyLocalhost";
    $database_mysql = "MyDatabase";
    $username_mysql = "blah";
    $password_mysql = "blah";
    $mysql = mysql_pconnect("$hostname_mysql", "$username_mysql", "$password_mysql") or die(mysql_error());
    if (!$mysql)
    {
    echo "Couldn't make a connection!";
    exit;
    }

    $db = mysql_select_db("$database_mysql",$mysql);
    if(!$db)
    {
    echo "The database disappeared!";
    mysql_close($mysql);
    exit;
    }
    echo"Ok!";
    if (empty($memberpass)|| empty($memberpass2)||empty($username)||empty($email))
    {
    die("Error! All fields are required. Please try again!");
    }

    if ($memberpass != $memberpass2)
    {
    die ("Error! Passwords do not match. Please try again!");
    }

    if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$',$email))
    {
    die("Error! $email does not look like valid email address.");
    }

    if (strlen($username)<2 || strlen($usrname)>50)
    {
    die ("Error! $username must be between 1 to 50 characters");
    }

    if (strlen($memberpass)<6||strlen($memberpass)>10)
    {
    die("Error! Password must be between 6 to 10 characters.");
    }

    $enc_pass = md5($memberpass);
    $query = "INSERT into members (member_id,username,email,memberpass) VALUES ('','$username','$email','$enc_pass')";
    $result= mysql_query($query) or die
    ("Could not execute query: $query." .mysql_error());

    if ($result)
    {
    session_register($username);
    setcookie("siteuser",$username,time()+604800);
    echo "Thanks! You have been registered.";

    }
    ?>

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, you mustn't send out anything before you write cookies, so
    PHP Code:
    echo"Ok!"
    may not be there.
    And perhaps something else...

    Just no html/text before messing with cookies
    - website

  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The answer as to why those errors are listed was given by website, but i thought i could tell you how to prevent them even if using output before sending them.

    One of the most powerfull things in PHP (which really is your very best friend, or soon will be), is Output Buffering.

    Now i'm not going to delve into it, but for example just do the following
    PHP Code:
    <?php
    // your php file begins here
    ob_start();

    // all of your code resides here

    //the end of your file
    ob_end_flush();
    ?>
    No more errors [img]images/smilies/wink.gif[/img]

    But please, before really using output buffering, read up on it over at php.net and do a search php+output+buffering.

    Hope this helps[img]images/smilies/wink.gif[/img]

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by datune
    The answer as to why those errors are listed was given by website, but i thought i could tell you how to prevent them even if using output before sending them.

    One of the most powerfull things in PHP (which really is your very best friend, or soon will be), is Output Buffering.

    Now i'm not going to delve into it, but for example just do the following
    PHP Code:
    <?php
    // your php file begins here
    ob_start();

    // all of your code resides here

    //the end of your file
    ob_end_flush();
    ?>
    No more errors [img]images/smilies/wink.gif[/img]

    But please, before really using output buffering, read up on it over at php.net and do a search php+output+buffering.

    Hope this helps[img]images/smilies/wink.gif[/img]
    oh man, this I didn't know, this is great, I had been creating my custom classes to do almost the exact thing and it already exists, great! Thanks a lot!
    - website

  5. #5
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can someone tell me why you would want to use output buffering?
    John

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    sn00zeville
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by datune
    PHP Code:
    <?php
    // your php file begins here
    ob_start();

    // all of your code resides here

    //the end of your file
    ob_end_flush();
    ?>
    No more errors [img]images/smilies/wink.gif[/img]

    But please, before really using output buffering, read up on it over at php.net and do a search php+output+buffering.

    Hope this helps[img]images/smilies/wink.gif[/img]
    Hi! Like where would I put the ob_start(); and ob_end_flush(); in my code?!.... Coz I tried to put it in between my codes... and still there are some warnings....

  7. #7
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    On the most part output buffering is used by slack people so they don't have to code around the fact that you can't send anything to the browser before headers/cookies.

    I'm sure they probably have a better use than that but I don't know any. Maybe I'm not thinking outside the box...

  8. #8
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by *seether*
    Hi! Like where would I put the ob_start(); and ob_end_flush(); in my code?!.... Coz I tried to put it in between my codes... and still there are some warnings....
    You would place ob_start(); at the top of your script, and ob_end_flush(); at the bottom.

    Packman:
    Well, it does save you a lot of (ugly) work, but ofcourse you could simply design your code not to output anything until in the end...

    I guess this is also good for caching because you can save the output to a file and then simply load the file when someone views the page, no big processing work...
    - website

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    sn00zeville
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's working now! Thanks!!


Bookmarks

Posting Permissions

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