Jump to content

[SOLVED] login script using sessions, wont work!


brophi

Recommended Posts

I know that there is a topic about this but it didnt help me very much. I am fairly new to php and i have no idea why my code is displaing these errors, any help would be greatly apretiated!!!!

 

the code:

<link href="style.css" rel="stylesheet" type="text/css" />
<?php 
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
session_start();
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo "Incorrect login details please try again.";
}
?>

 

and the error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\site\login.php:2) in C:\xampp\htdocs\site\login.php on line 7

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\login.php:2) in C:\xampp\htdocs\site\login.php on line 8

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\login.php:2) in C:\xampp\htdocs\site\login.php on line 10

 

Thank you and im im sorry again...

Link to comment
Share on other sites

Try this:

 

<?php 
session_start();
echo '<link href="style.css" rel="stylesheet" type="text/css" />';
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo "Incorrect login details please try again.";
}
?>

Link to comment
Share on other sites

Thanks for the quick reply! but sorry, its still not working... I did manage to get rid of some of the errors, tahnks to your help

 

here is my code NOW:

<?php session_start(); ?>
<link href="style.css" rel="stylesheet" type="text/css" />
<?php 
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo "Incorrect login details please try again.";
}
?>

 

And here are the errors...

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\login.php:3) in C:\xampp\htdocs\site\login.php on line 8

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\login.php:3) in C:\xampp\htdocs\site\login.php on line 10

Link to comment
Share on other sites

Your code should be like this

 

<?php 
session_start();
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo '<link href="style.css" rel="stylesheet" type="text/css" />'
echo "Incorrect login details please try again.";
}
?>

Link to comment
Share on other sites

GRRR! Its still not working! i used the code above:

<?php 
session_start();
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo '<link href="style.css" rel="stylesheet" type="text/css" />'
echo "Incorrect login details please try again.";
}
?>

 

but that didnt work:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\xampp\htdocs\site\login.php on line 12

 

I fixed that error:

<?php 
session_start();
include("connect.php");
$sql = mysql_query("SELECT * FROM users WHERE username='".addslashes($_POST['username'])."'") or die("username was in correct. MySQL said".mysql_error());
$result = mysql_fetch_array($sql);
if($result['password'] == sha1($_POST['password'])){
header("Cache-control: private");
$_SESSION["sessioname"] = $_POST['username'];
header("location: protected.php");
}else{
echo "Incorrect login details please try again.";
echo '<link href="style.css" rel="stylesheet" type="text/css" />';
}
?>

 

BUT THEN I GET THIS ERROR AGAIN!!

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\connect.php:2) in C:\xampp\htdocs\site\login.php on line 7

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\site\connect.php:2) in C:\xampp\htdocs\site\login.php on line 9

 

PLEASE HELP! this is very frustrating!

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.