Php vs. Me

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I think I may kill my computer if I can't fix this.

Problem:

Code:
Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\checklogin.php:1) in C:\Program Files\xampp\htdocs\HIV\checklogin.php on line 12

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\HIV\checklogin.php:1) in C:\Program Files\xampp\htdocs\HIV\checklogin.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\checklogin.php:1) in C:\Program Files\xampp\htdocs\HIV\checklogin.php on line 14

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

Login.php is just a form that's action is checklogin.php.

Contents of checklogin.php:
Code:
<?php
include ("db.php");
$tbl_name="users"; 
mysql_connect("$host", "$user", "$password")or die("cannot connect"); 
mysql_select_db("$database")or die("cannot select DB");
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

Contents of login_success.php:
Code:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>

db.php simply contains host, username, pwd and table name for mysql.

I've had similar problems in the past due to whitespaces... but this doesn't appear to be it... anyone got any ideas?
 
Inquisitor said:
You sure there are no new lines between the beginning of the file and the opening PHP tag?

This is the thing that is absolutely annoying me... none... :S

EDIT - I just rewrote the file, no whitespaces,

It is definately two problems here, session proiblems and then header info mod. errors... any ideas?
 
Last edited:
The problem is linked to:

Code:
session_register("myusername");
session_register("mypassword");
header("Location: index.php");
 
have u got this line in the code? You need to start the session first:

session_start();

jd
 
Also happening in index.php - thrice!


Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 15


Code:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
$file = "hivcounter.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
fclose($open);
if (!isset($_COOKIE['simplecount'])) {
	$open = fopen($file, "w");
	$count++;
	fwrite($open, $count);
	fclose($open);
	setcookie("simplecount","Counted!",time()+66600000);
}
?>

JonD said:
have u got this line in the code? You need to start the session first:

session_start();

jd

Yes. At the very top of index.php as above...

Inquisitor said:
Does db.php contain any white space?

None :S
 
Last edited:
Any further ideas anyone?

index.php:

Code:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
$file = "hivcounter.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
fclose($open);
if (!isset($_COOKIE['simplecount'])) {
	$open = fopen($file, "w");
	$count++;
	fwrite($open, $count);
	fclose($open);
	setcookie("simplecount","Counted!",time()+66600000);
}
?>
<p class="style2">Total Visitors: <?PHP echo "$count"; ?></p>

login.php:
Code:
<form name="form1" method="post" action="checklogin.php">

checklogin.php:
Code:
<?php
include ("db.php");
$tbl_name="users"; 
mysql_connect("$host", "$user", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
header("Location: index.php");
}
else {
echo "Wrong Username or Password";
}
?>

ERRORS:

Code:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\HIV\index.php:1) in C:\Program Files\xampp\htdocs\HIV\index.php on line 16
 
Last edited:
Right... played and played and even installed mysql locally to check it all...

SAME problem so it isn't my host!

Rage!!!!!!

Any ideas peeps?
 
Edit. Rewrote it in a different way... GOT THE SAME PROBLEM.

Copy and pasted the contents of the files into new files.. problems disappear... WHAT ON EARTH?
 
Nope...

It is to make something I'm doing eaasy... and ironically save me time!

This was supposed to be an oppurtunity to do a fancy login (that wasn't meant to be secure).

Scrapped it. Rewrote it as I said, same problems! Copy/pasted into new files... and it worked!

What is your assignment?
 
It looks to me like you are not starting the session in checklogin.php. If the checklogin.php logs people in, then you are setting a session var without ever starting the session. So the first time the session is started is in the index.php page.

As for the session problem in index.php. Is your browser set up to except cookies.

On a sidenote i use require_once() rather that include() because as your site gets better you can easily have files that link to each other many times without realising and you run into all kinds of problems because of it and you won't have a clue why you are getting the errors.
 
If saving them in new files got rid of the problems, my money would be on the problem being the UTF BOM at the start of the file, which is sent to the user (thus sending page headers and therefore stopping you from modifying the headers) despite never being displayed.

Either save as ANSI, or UTF without the BOM (most text editors should let you do this).
 
Back
Top Bottom