Login.php

Ex8roS

New Member
Messages
8
Reaction score
0
Points
0
So i am trying to make a Register/Login system on my site. Note that i didn't write the scripts. The Registration works fine and the user info are stored in the database. But when i go to the login page i get these errors:

PHP:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/kg/public_html/login.php:1) in /home/kg/public_html/login.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/kg/public_html/login.php:1) in /home/kg/public_html/login.php on line 2

My original script for login.php was:

PHP:
<?php
session_start();

$conn = mysql_connect('localhost', '******, '*****') or die(mysql_error());
mysql_select_db('*****', $conn); 



if(isset($_GET['try'])) {
 
	If(empty($_POST['username']) OR empty($_POST['password'])) {
 
		echo 'Please fill in all the required fields!';
	} else {
		$username = mysql_real_escape_string($_POST['username']);
		$password = md5($_POST['password']);
 
		$query = mysql_query("SELECT id FROM login
					   WHERE username = '" . $username . "' 
					   AND password = '" . $password . "'
					  ") or die(mysql_error());
 
		list($user_id) = mysql_fetch_row($query);

		if(empty($user_id)) {
			echo 'No combination of username and password found.';
		} else {
			
			$_SESSION['user_id'] = $user_id;
 
			header('location: index.html');
		}		
	}
}

?>

After some search i was told to add ob_start(); after session_start(); and ob_end_flush(); just before the ?> tag. But that didnt work :/

Since i am not that fond of php and my experience with it is very limited i couldnt get past this problem, although i noticed that the script isnt checking if the user is already logged in. So i tried to alter the script with the hope that if it somehow checked if the user is logged in this problem would be fixed.

The result was that:

PHP:
<?php
session_start();
ob_start();

$conn = mysql_connect('localhost', '******', '******') or die(mysql_error());
mysql_select_db('*****', $conn); 

if(!(isset($_SESSION['user_id']))) {

if(isset($_GET['try'])) {
 
	If(empty($_POST['username']) OR empty($_POST['password'])) {
 
		echo 'Please fill in all the required fields!';
	} else {
		$username = mysql_real_escape_string($_POST['username']);
		$password = md5($_POST['password']);
 
		$query = mysql_query("SELECT id FROM login
					   WHERE username = '" . $username . "' 
					   AND password = '" . $password . "'
					  ") or die(mysql_error());
 
		list($user_id) = mysql_fetch_row($query);

		if(empty($user_id)) {
			echo 'No combination of username and password found.';
		} else {
			
			$_SESSION['user_id'] = $user_id;
 
			header('location: index.html');
		}		
	}
}else{
	echo 'You are already logged in.';
}

}
ob_end_flush();
?>

But it still isnt working. However it is printing the "You are already logged in" message under the errors.

If someone doesnt mind looking into it i would appreciate it. And dont worry i will understand whatever u tell me, i have some experience with programming just not that much with php yet.

My site is aniwalls.exofire.net. Under Constuction ofc.
 
Last edited:

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
/***************Moving to Programming Help****************/
 

Ex8roS

New Member
Messages
8
Reaction score
0
Points
0
ehh i was calling it from a form in the same file (login.php). But now i changed the files so that login.php contains only php code and login.html calls the login.php through a form.

login.html form:

HTML:
<form action="login.php?try=true" method="post" >

<label for="Username">Username:</label> <input type="text" id="Username" name="username"><br>
<br>
<label for="Password">Password:</label> <input type="password" id="Password" name="password"><br>
<br>
<input type="submit" value="Login!" />
</form>

but now i get an extra error:

PHP:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/kg/public_html/login.php:1) in /home/kg/public_html/login.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/kg/public_html/login.php:1) in /home/kg/public_html/login.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/kg/public_html/login.php:1) in /home/kg/public_html/login.php on line 32
 

lordskid

New Member
Messages
41
Reaction score
0
Points
0
try to observe your character encoding try to change your file to a utf-8 or whichever you used to create the file before upload.
 

sybregunne

Member
Messages
54
Reaction score
0
Points
6
do that if you are sure that there is no other character before your

<?php
session_start();

I have read that character sets can provide those magic boxes before your file so that your session start will give an error.
 

Ex8roS

New Member
Messages
8
Reaction score
0
Points
0
The file was saved as utf-8 and i uploaded it like that, and i made sure there were no characters or lines before <?php or aftrer ?> since i am aware of this causing a problem.
 

mfurqanabid

New Member
Messages
37
Reaction score
0
Points
0
Create a new file name login.php and paste your code in this file.

Be sure, no space or any char before or end of the php tags (<?php ?>).

Some times some spaces, new line or file takes this problem.

Check it.
Edit:
it is due to some command like header and session cannot start after spacing or enters like
-------------


<?php
session_start();
?>

its

----------
<?php
session_start();
?>


(----------) shows file start from here
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I would consider removing the ob_start and ob_end_flush functions
 

Ex8roS

New Member
Messages
8
Reaction score
0
Points
0
Create a new file name login.php and paste your code in this file.

Be sure, no space or any char before or end of the php tags (<?php ?>).

Some times some spaces, new line or file takes this problem.

Check it.
Edit:
it is due to some command like header and session cannot start after spacing or enters like
-------------


<?php
session_start();
?>

its

----------
<?php
session_start();
?>


(----------) shows file start from here

Yes i have heard that this causes the problem and as i said i checked several times for blank spaces and characters. But i will do what u suggest after my site starts working since i cant access it cuz of the server move.

I would consider removing the ob_start and ob_end_flush functions


Well it didnt work without them either, i put them there cuz i was told that it may solve the problem.

Anyway its not that i cant try another script but i want to get to the bottom of this.

Is there a chance that the problem is server side (php limitations) or some files do not have the required access?
 
Last edited:

Ex8roS

New Member
Messages
8
Reaction score
0
Points
0
ok fixed. i copied the data to a new login.php file and removed the ob_start / end. it seems to be ok now.

thanks for your help :)
 

itman647

New Member
Messages
45
Reaction score
0
Points
0
This has pissed me off before too, you have to make sure that you're using the right character set. To make sure that there is no sqaure boxes before your script you have to...

...Login to your cPanel, open up the File Manager and click Go. Locate your .php file and click on it so that the row where your file is, is highlighted blue. Click on Code Editor at the top. Once the window has opened, look at the top of the page, where it says encoding, click on the drop down menu and select either UTF-8 or US-ASCII, now click Open, when you view the php file in either one of these sets you should notice some square boxes before the script, now remove them and click Save Changes...

hope this helps you out in the future...

:biggrin:
 
Last edited:

Kouen

New Member
Messages
8
Reaction score
0
Points
0
if it is an extra character getting appended on after your ?> tag, you could always leave out the ?> tag, provided the file is pure php. That will prevent a lot of errors, and it isn't required anyway.
 
Top