Jump to content

Session Start() error


MissPurr

Recommended Posts

I have a basic login that was working just fine on my local WAMPP server, and now I moved it over to my host and the login is throwing errors.

 

It's the same version of PHP, and Mysql so that isn't the problem.

 

Errors:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/qwo/bon-temps/index.php:3) in /home/qwo/bon-temps/login.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/qwo/bon-temps/index.php:3) in /home/qwo/bon-temps/login.php on line 31

 

Line 19:

session_start();

 

Line 31:

header("Location: myaccount.php");

 

Code:

<?php
include 'dbc.php';

$user_email = mysql_real_escape_string($_POST['email']);

if ($_POST['Submit']=='Login')
{
$md5pass = md5($_POST['pwd']);
$sql = "SELECT username,email FROM players WHERE
            email = '$user_email' AND
            password = '$md5pass' AND user_activated='1'";

$result = mysql_query($sql) or die (mysql_error());
$num = mysql_num_rows($result);

    if ( $num != 0 ) {

        // A matching row was found - the user is authenticated.
       session_start();
   list($md5pass,$user_email) = mysql_fetch_row($result);
	// this sets variables in the session
	$_SESSION['user']= $user_email;
	$_SESSION['pname']= $row['username'];


	if (isset($_GET['ret']) && !empty($_GET['ret']))
	{
	header("Location: $_GET[ret]");
	} else
	{
	header("Location: myaccount.php");
	}
	//echo "Logged in...";
	exit();
    }

header("Location: login.php?msg=Invalid Login");
//echo "Error:";
exit();
}

?>
<link href="styles.css" rel="stylesheet" type="text/css">

<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>


<p> </p><table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#d5e8f9" class="mnuheader" >
<div align="center"><font size="5"><strong>Login
        Members</strong></font></div></td>
  </tr>
  <tr>
    <td bgcolor="#e5ecf9" class="mnubody"><form name="form1" method="post" action="">
        <p> </p>
        <p align="center">Your Email
          <input name="email" type="text" id="email">
        </p>
        <p align="center"> Password:
          <input name="pwd" type="password" id="pwd">
        </p>
        <p align="center">
          <input type="submit" name="Submit" value="Login">
        </p>
        <p align="center"><a href="register.php">Register</a> | <a href="forgot.php">Forgot</a></p>
      </form></td>
  </tr>
</table>

Link to comment
Share on other sites

you need to exit your headers  ;)

 

<?php
include 'dbc.php';

$user_email = mysql_real_escape_string($_POST['email']);

if ($_POST['Submit']=='Login')
{
$md5pass = md5($_POST['pwd']);
$sql = "SELECT username,email FROM players WHERE
            email = '$user_email' AND
            password = '$md5pass' AND user_activated='1'";

$result = mysql_query($sql) or die (mysql_error());
$num = mysql_num_rows($result);

    if ( $num != 0 ) {

        // A matching row was found - the user is authenticated.
       session_start();
      list($md5pass,$user_email) = mysql_fetch_row($result);
      // this sets variables in the session
      $_SESSION['user']= $user_email;
      $_SESSION['pname']= $row['username'];


      if (isset($_GET['ret']) && !empty($_GET['ret']))
      {
      header("Location: $_GET[ret]");
      exit();
      } else
      {
      header("Location: myaccount.php");
      exit();
      }
      //echo "Logged in...";
      exit();
    }

header("Location: login.php?msg=Invalid Login");
//echo "Error:";
exit();
}

?>
<link href="styles.css" rel="stylesheet" type="text/css">

<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>

 

 

Link to comment
Share on other sites

 

 

The reason your code worked on your local development system is because the output_buffering setting is ON in php.ini. To save time, I recommend that you turn this setting off (stop and start your web server to get any changes made to php.ini to take effect) and get your code working without errors on your development system, then move your code to your live server.

 

The error is saying where your code is sending content to the browser that is preventing the headers from working. All headers must be sent before a single character is sent to the browser.

Link to comment
Share on other sites

This should fix it:

 

<?php
session_start();
?><html>
<body>
<?php
include ('login.php');
?>
<p /><p />
<a href="register.php">Register if you are not a member</a>
</body>
</html>

 

Even that html tag at the start is enough to interfere with sessions, so you need to start them first.

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.