Jump to content

Problem with check cookie/post function


JP128

Recommended Posts

I get the problem

[code] PHP Warning:  Cannot modify header information - headers already sent by (output started at /usr/htdocs/config.php:49) in /htdocs/config.php on line 84
[/code]

and thats it.. here is my current script.
I showed what # the lines are... <-=-=-=-  ##  -=-=-=->
[code]
<?php
################### Database Connect #############
function dbConnect() { //Start of dbConnect
  mysql_connect("localhost", "*****", "********");
  mysql_select_db("*****");
}

################### User Auth ####################
function userAuth() {
  //Connect to the database.
  dbConnect(); // <-=-=-=- THIS IS LINE 49 -=-=-=->
  echo"Connected to database.<br>";
  //Check to see if the cookies are set.
  if((isset($_COOKIE['username']))&&(isset($_COOKIE['password']))){ //beginning of 2
      echo"The cookies are set.<br>";
    //Set the username, and password values because the cookie is set.
    $username = $_COOKIE['username']; // <-=-=-=- THIS IS LINE 84 -=-=-=->
    $password = $_COOKIE['password']; // <-=-=-=- THIS IS LINE 85 -=-=-=->
    echo"Cookie values added to the variables.";
    //Check the cookie variables against the registry table.
    $checkCookie = mysql_query("SELECT username,password FROM `registry` WHERE username='$username' AND password=md5('$password')");
    //Get the number of rows that $checkCookie looks up.
    $cookieRows = mysql_num_rows($checkCookie);
        if($cookieRows > 0){
        echo"That information exists in database!<br>";
            }else{
        echo"That information does not exist in database!<br>";
        //Show the login form because the cookie is invalid
        loginForm();
        exit;
            }
        }else{
      echo"That cookie doesn't exist. Checking to see if POST does...<br>";
      //Check to see if the username, and password are set because the cookies aren't.
        if((isset($_POST['username']))&&(isset($_POST['password']))){ // if POST
        echo"Post is in fact set.<br>";
        //Set the variables, because the POST is set.
        $username = $_POST['username'];
        $password = $_POST['password'];
        echo"Set the variables from POST<br>";
        //Check the POST variables against the table Registry.
        $checkPost = mysql_query("SELECT username,password FROM registry WHERE username='$username' AND password=md5('$password')");
        //Check on how many rows $checkPost looks up.
        $postRows = mysql_num_rows($checkPost);
            if($postRows > 0){
            echo"Those POST variables are in the database.<br>";
            setcookie("username",$username,time()+3600);
            setcookie("password",md5($password),time()+3600);
            echo"Set the cookie values...<br>";
              }else{
            echo"Showing Login form.<br>";
            loginForm();
            exit;
            }
        }
      }
  }
[/code]

I added all of the echo's in there to see how far it goes.
I don't know what is going on, and why it doesn't work. If someone could take a look at it, that would be great... I also searched around alot and saw tons of people with the whitespace problem, but I don't think that that is my problem.
Link to comment
https://forums.phpfreaks.com/topic/30101-problem-with-check-cookiepost-function/
Share on other sites

you cannot have any html output before your setcookie function call.  no echos, no blank lines above your php tags, no nothing.  or you can look into [url=http://us2.php.net/ob_start]ob_start()[/url] if you need to have html output before it.
[CODE]
<?php
function userAuth() {
ob_start();
//Connect to the database.
dbConnect();
echo"Connected to database.<br>";
//Check to see if the cookies are set.
if((isset($_COOKIE['username']))&&(isset($_COOKIE['password']))){ //beginning of 2
echo"The cookies are set.<br>";
//Set the username, and password values because the cookie is set.
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
echo"Cookie values added to the variables.";
//Check the cookie variables against the registry table.
$checkCookie = mysql_query("SELECT username,password FROM `registry` WHERE username='$username' AND password=md5('$password')");
Get the number of rows that $checkCookie looks up.
$cookieRows = mysql_num_rows($checkCookie);
if($cookieRows > 0){
echo"That information exists in database!<br>";
}else{
echo"That information does not exist in database!<br>";
//Show the login form because the cookie is invalid
loginForm();
exit;
}
}else{
echo"That cookie doesn't exist. Checking to see if POST does...<br>";
//Check to see if the username, and password are set because the cookies aren't.
if((isset($_POST['username']))&&(isset($_POST['password']))){ // if POST
echo"Post is in fact set.<br>";
//Set the variables, because the POST is set.
$username = $_POST['username'];
$password = $_POST['password'];
echo"Set the variables from POST<br>";
//Check the POST variables against the table Registry.
$checkPost = mysql_query("SELECT username,password FROM registry WHERE username='$username' AND password=md5('$password')");
//Check on how many rows $checkPost looks up.
$postRows = mysql_num_rows($checkPost);
if($postRows > 0){
$username = $_POST['username'];
$password = $_POST['password'];
setcookie("username",$username,time()+3600);
setcookie("password",md5($password),time()+3600);
echo"Those POST variables are in the database.<br>";
echo"Set the cookie values...<br>";
}else{
echo"Showing Login form.<br>";
loginForm();
exit;
}
}
}
ob_end_flush();
}
?>
[/CODE]
okay, i see that you have ob_start() and ob_end_flush() inside a function, which means that the script you have provided is part of a larger script.  ob_start() has to be at the beginning of the entire script and ob_end_flush() has to be at the end of the entire script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.