Jump to content

Recommended Posts

Im creating a login form but everytime i try to output a header or start a session i get errors:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /web/users/e5034824/Test/confirmlogin.php:1) in /web/users/e5034824/Test/confirmlogin.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /web/users/e5034824/Test/confirmlogin.php:1) in /web/users/e5034824/Test/confirmlogin.php on line 2

 

 


<?php
session_start();
$username = $_POST['myusername'];
$password = $_POST['mypassword'];

require 'common.php';

$q = "SELECT id FROM members WHERE username = '$username' AND password = '$password'";

$result = $mysqli->query($q) or die(mysqli_error());

if (mysqli_num_rows($result) == 1) {
$user = $result->fetch_array(MYSQLI_NUM);
$_SESSION['admin'] = $user[0]; // Sets the session to the user's ID
} else {
$message = "User Name Not Recongised, Please Try Again!";
echo $message .  ' <meta HTTP-EQUIV="refresh" content=5;url="login.php"/>';
}
?>

 

I always get the header error message but have no clue on how to fix it any ideas?

Read this

 

the problem with the code below is the white space at the start.. either that or your including it from another file that has output!

 

// <---- remove this white space --->
<?php
session_start();
$username = $_POST['myusername'];

 

yes, any output to the browser before using headers will cause errors.

 

Remember that header() must be called before any actual output is sent' date=' either by normal HTML tags, blank lines in a file, or from PHP. [b']It is a very common error[/b] to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
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.