Link to home
Create AccountLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

PHP Error Message Question

Hi guys,

I was suggested by an expert to use this script from the recommended part of the EE site but am having some problems integrating it.

I've setup the config.php page, and the login.php page and am calling it from the login.php page like so:

require_once('config.php');

When I load the login page it gives this error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/12/10022112/html/login.php:8) in /home/content/12/10022112/html/config.php on line 13

<?php // config.php

// WHEN WE ARE DEBUGGING OUR CODE, WE WANT TO SEE ALL THE ERRORS!
error_reporting(E_ALL);

// REQUIRED FOR PHP 5.1+ - Fill this in later
date_default_timezone_set('America/Chicago');

// THE LIFE OF THE "REMEMBER ME" COOKIE
define('REMEMBER', 60*60*24*7); // ONE WEEK IN SECONDS

// WE WANT TO START THE SESSION ON EVERY PAGE
session_start();

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "bubblesdb.db.10022112.hostedresource.com"; // PROBABLY THIS IS OK
$db_name = "bubblesdb";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "bubblesdb";
$db_word = "xxx";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die("NO DATA BASE $db_name");
}

// DEFINE THE ACCESS CONTROL FUNCTION
function access_control($test=FALSE)
{
    // REMEMBER HOW WE GOT HERE
    $_SESSION["entry_uri"] = $_SERVER["REQUEST_URI"];

    // IF THE UID IS SET, WE ARE LOGGED IN
    if (isset($_SESSION["uid"])) return $_SESSION["uid"];

    // IF WE ARE NOT LOGGED IN - RESPOND TO THE TEST REQUEST
    if ($test) return FALSE;

    // IF THIS IS NOT A TEST, REDIRECT TO CALL FOR A LOGIN
    header("Location: login.php");
    exit;
}

// DEFINE THE "REMEMBER ME" COOKIE FUNCTION
function remember_me($uuk)
{
    // CONSTRUCT A "REMEMBER ME" COOKIE WITH THE UNIQUE USER KEY
    $cookie_name    = 'uuk';
    $cookie_value   = $uuk;
    $cookie_expires = time() + date('Z') + REMEMBER;
    $cookie_path    = '/';
    $cookie_domain  = NULL;
    $cookie_secure  = FALSE;
    $cookie_http    = TRUE; // HIDE COOKIE FROM JAVASCRIPT (PHP 5.2+)

    // SEE http://us3.php.net/manual/en/function.setcookie.php
    setcookie
    ( $cookie_name
    , $cookie_value
    , $cookie_expires
    , $cookie_path
    , $cookie_domain
    , $cookie_secure
    , $cookie_http
    )
    ;
}



// DETERMINE IF THE CLIENT IS ALREADY LOGGED IN BECAUSE OF THE SESSION ARRAY
if (!isset($_SESSION["uid"]))
{

    // DETERMINE IF THE CLIENT IS ALREADY LOGGED IN BECAUSE OF "REMEMBER ME" FEATURE
    if (isset($_COOKIE["uuk"]))
    {
        $uuk = mysql_real_escape_string($_COOKIE["uuk"]);
        $sql = "SELECT uid FROM EE_userTable WHERE uuk = '$uuk' LIMIT 1";
        $res = mysql_query($sql);

        // IF THE QUERY SUCCEEDED
        if ($res)
        {
            // THERE SHOULD BE ONE ROW
            $num = mysql_num_rows($res);
            if ($num)
            {
                // RETRIEVE THE ROW FROM THE QUERY RESULTS SET
                $row = mysql_fetch_assoc($res);

                // STORE THE USER-ID IN THE SESSION ARRAY
                $_SESSION["uid"] = $row["uid"];

                // EXTEND THE "REMEMBER ME" COOKIE
                remember_me($uuk);
            }
        }
    }
}

?>

Open in new window


Any help would be greatly appreciated,

Thanks,

Derek
Avatar of hielo
hielo
Flag of Wallis and Futuna image

FYI: The problem is happening because in login.php (around line 8), you are sending content/output to the client and later on you are calling session_start().  You MUST call session_start() before you send any output/content to the browser.

To "fix" this, on login.php, call session_start() first, then include the config.php file -ex:
<?php
//login.php
session_start();
...
require_once('config.php');
...

Open in new window


Then in config.php get rid of line 13 => session_start(); because you already called it at the beginning of login.php.
Avatar of TLN_CANADA

ASKER

Thank you hielo for your help. Now it's giving this error instead:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/12/10022112/html/login.php:8) in /home/content/12/10022112/html/login.php on line 9
you need to remove session_start() from config.php or login.php

if you remove from login.php, make sure config.php is included in the first line itself

if you remove from config.php, make sure to have session_start before config.php is included ie, in the first line itself

you cannot have start session after some output is sent to browser. in your case either login.php or config.php is sending some output before session_start is called.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks guys,

I have the script working now so that it adds to the DB correctly and logs me into the homepage:

The line:

   echo "<br/>CLICK <a href=\"/\">HERE</a> TO GO TO THE HOME PAGE";

How do I change this so it takes me to the journal.php page instead? I've played around with this but don't know the correct syntax for PHP for href.

Thank you,

Derek
href is HTML, not PHP.  Maybe something like this:
echo '<br/>CLICK <a href="journal.php">HERE</a> TO GO TO THE HOME PAGE';

Open in new window

Thanks Ray, something else a little funky is happening. On the login page I have it setup as follows:

  // REDIRECT TO THE ENTRY PAGE OR TO THE HOME PAGE
        if (isset($_SESSION["entry_uri"]))
        {
            header("Location: {$_SESSION["entry_uri"]}");
            exit;
        }
        else
        {
            header("Location: http://www.clearthemirror.com/journal.php/");
            exit;
        }
    } // END OF SUCCESSFUL VALIDATION
    else
    {
        echo "SORRY, VALIDATION FAILED USING $uid AND $pwd \n";
    }
} // END OF FORM PROCESSING - PUT UP THE LOGIN FORM

Open in new window


Something strange happens though when a correct user id and password is entered. It should takes me to the journal.php page but instead takes me to the main index page. Although it is the main index page of the site, it does display the journal.php page in the web url bar. Do you know why it is doing this?

Thank you,

Derek
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
On the journal page I only have the short PHP code that should check if the user is logged in:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<p>Journal - Date - TODAY'S DATE </p>
<form id="form1" name="form1" method="post" action="">
  <label>
  <textarea name="textarea" cols="100" rows="12"></textarea>
  </label>
</form>
<form id="form2" name="form2" method="post" action="">
  <label>
  <input type="submit" name="Submit2" value="Close without Saving" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Save and Return" />
  </label>
</form>
</body>
</html>

Open in new window


How can I check if $_ SESSION["entry_uri"] has a value if it doesn't load the journal.php page?

Thanks,

Derek
Sorry, here is the PHP script at the top of the page:

<?php
require_once('config.php'); 
access_control();
?>

Open in new window