Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2005, 06:00 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with session_start()

I am using PHP with mySQL to allow users to login. I am using $_SESSION[] to pass variables such as there username, password, as well as the standard.

The script works fine for the first page but on my second page I get the following errors:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at \\brink-premfs1\sites\premium3\exitcharde\webroot\forum\my Posts.php:1) in \\brink-premfs1\sites\premium3\exitcharde\webroot\forum\ch eck_login.php on line 2

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at \\brink-premfs1\sites\premium3\exitcharde\webroot\forum\my Posts.php:1) in \\brink-premfs1\sites\premium3\exitcharde\webroot\forum\ch eck_login.php on line 2

I know that I need to have session_start() before anything is outputted. I don't think that is the issue. I can't seem to figure out why else I would get these messages though.

Here is my code:
----------------------------------------------------------------------------
Script from page giving errors (myPosts.php)
----------------------------------------------------------------------------
<?php
require 'connect.php';

if ($logged_in == 0) {
    die('Sorry you are not logged in. <a href="login.php" target="_parent">Click here</a> to log in.');
}
?>
<!--
<HTML>
<HEAD>......Content.....

----------------------------------------------------------------------------
connect.php
----------------------------------------------------------------------------
<?php

$conn = new COM('ADODB.Connection');
$conn->Open("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=mysql2.brinkster.com;DATABASE=exitcharde;UI D=exitcharde;PWD=joecharde;");

include('check_login.php');
?>
----------------------------------------------------------------------------
check_login.php
----------------------------------------------------------------------------

<?php
session_start();

if (!isset($_SESSION['userName']) || !isset($_SESSION['password'])) {
    $logged_in = 0;
    return;
} else {
    if(!get_magic_quotes_gpc()) {
        $_SESSION['userName'] = addslashes($_SESSION['username']);
    }


    // addslashes to session username before using in a query.
    $result = @$conn->Execute("SELECT password FROM users WHERE username = '".$_SESSION['userName']."'") or die('Database Error. Please Try Again');

    if($result->EOF){
        $logged_in = 0;
        unset($_SESSION['userName']);
        unset($_SESSION['password']);
    }

    $ret_pass = stripslashes($result->fields[0]->value);
    $_SESSION['password'] = stripslashes($_SESSION['password']);



    //compare:



    if($_SESSION['password'] == $ret_pass) {
        // valid password for username
        $logged_in = 1;
    } else {
        $logged_in = 0;
        unset($_SESSION['userName']);
        unset($_SESSION['password']);
        // kill incorrect session variables.
    }
}


// clean up
unset($ret_pass);

$_SESSION['userName'] = stripslashes($_SESSION['userName']);

?>
----------------------------------------------------------------------------
login.php
----------------------------------------------------------------------------

<?php

// database connect script.

require 'connect.php';

if($logged_in == 1) {
    echo ;
    die('<html><head><title>Login</title><SCRIPT LANGUAGE = "JavaScript">function getURL() {var url = "http://www.askmeaboutexit.com/forum/";parent.window.location.href= url} </SCRIPT></head><html><body onload="javascript: getURL()">'.$_SESSION['name'].', You are already logged in.');

}


?>
<html>
<head>
<title>Login</title>
<SCRIPT LANGUAGE = "JavaScript">

function getURL() {
var url = 'http://www.askmeaboutexit.com/forum/';
parent.window.location.href= url

}


</SCRIPT>
</head>

<?php

if (isset($_POST['submit'])) { // if form has been submitted


    /* check they filled in what they were supposed to and authenticate */
    if(!$_POST['userName'] | !$_POST['password']) {
        die('You did not fill in a required field. <a href="login.php" target="_parent">Click here</a> to return to Login.');
    }

    // authenticate.

    if (!get_magic_quotes_gpc()) {
        //$_POST['userName'] = addslashes($_POST['userName']);
    }

    $check = $conn->Execute("SELECT first, userName, password, login_count FROM users WHERE userName = '".$_POST['userName']."'");

    if($check->EOF){
        die('That Username Does Not Exist In Our System. <a href="login.php" target="_parent">Click here</a> to return to Login.');
    }


    // check passwords match

    $_POST['password'] = stripslashes($_POST['password']);
    $ret_pass = stripslashes($check->fields[2]->value);
    $_POST['password'] = /*md5(*/$_POST['password'];

    if ($_POST['password'] != $ret_pass) {
        die('Incorrect password, please try again.');
    }

    // if we get here username and password are correct,
    //register session variables and set last login time.
    $count = $check->fields[3]->value;
    $count = $count+1;
    $date = date('H:i M d, Y');

    $update_login = $conn->Execute("UPDATE users SET login_count='".$count."', last_login = '".$date."' WHERE username = '".$_POST['userName']."'");

    $_POST['userName'] = stripslashes($_POST['userName']);
    $_SESSION['userName'] = $_POST['userName'];
    $_SESSION['password'] = $_POST['password'];
    $_SESSION['name'] = stripslashes($check->fields[0]->value);
    $logged_in = 1;


?>
<body onload="javascript: getURL()">
<h1>Logged In</h1>
<p>Welcome back <?php echo $_SESSION['name']; ?>, you are currently logged in.</p>

<?php

} else { // if form hasn't been submitted

?>
<h1>Login</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
  <tr>
    <td align="right">User Name:</td>
    <td ><input name="userName" type="text" size="25"></td>
  </tr>
  <tr>
    <td align="right">Password:</td>
    <td><input name="password" type="password" size="25"></td>
  </tr>
  <tr>
    <td></td>
    <td align="right"><input name="submit" type="submit" value="Login"></td>
  </tr>
</table>
</form>
<?php
}
?>
</body>
</html>
----------------------------------------------------------------------------


Again...All of this works for the first page (index.php) which starts just like myPosts.php


----------------------------------------------------------------------------
index.php
----------------------------------------------------------------------------
<?php

require 'connect.php');

if ($logged_in == 0) {
    die('Sorry you are not logged in. <a href="login.php" target="_parent">Click here</a> to log in.');
}


?>
<HTML>
<HEAD>...........content............
----------------------------------------------------------------------------

Any help you can give would be great!

justinr@charde.com
 
Old April 27th, 2005, 07:49 PM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please POST in only one FORUM!

----------------
Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq
 
Old April 27th, 2005, 07:52 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I can't seem to get results no matter how many forums I post in.

 
Old April 28th, 2005, 12:18 AM
Authorized User
 
Join Date: Apr 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jmukesh Send a message via Yahoo to jmukesh
Default

Hi ,

This two lines should always be there before your code starts

 ob_start();
 session_start();

ob_start functions buffers all the html headers and will stop the error which your are getting.

This two lines should be the first lines in any of your php scripts.

Thanx


 
Old May 8th, 2005, 05:09 PM
Authorized User
 
Join Date: May 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

i have the same problem. I am actually working from an example inthe PHP book. I got the problem mentioned here with the two error messages coming up. i did as jmukesh said, by including the two lines before any code starts, but.. i still get the error message?

Anyone help please

Steve






Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning: session_start(): tanybrw989 Beginning PHP 9 May 10th, 2009 06:16 PM
session_start() changusee2k PHP How-To 2 February 11th, 2006 05:32 AM
Problem with session_start() jmrdeuce32 Pro PHP 5 June 10th, 2005 01:53 PM
Problem with Session_Start() jmrdeuce32 PHP How-To 2 April 29th, 2005 12:32 AM
session_start() problem cfemocha BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 28th, 2004 08:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.