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
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.