Page 1 of 1

Header Problems/Session Register

Posted: Thu Oct 23, 2008 4:47 pm
by oscardog
Well im relatively new to PHP, about a week and i keep getting the same recurring errors on login scripts. Normally similar, so fix it once i can fix em all i guess. Here is my code, they submit a form from a previous page and then go to checklogin.php (from main_login.php):

Code: Select all

 
<?php
$host="mysql2.100ws.com"; // Host name 
$username="ashpea1_160714"; // Mysql username 
$password="qwerty"; // Mysql password 
$db_name="ashpea1_160714"; // Database name 
$tbl_name="members"; // Table name 
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
 
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count== 1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
 
(Removed HTML elements)

I get the following errors:

Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at /home/www/searchyouringredients.100webspace.net/checklogin.php:9) in /home/www/searchyouringredients.100webspace.net/checklogin.php on line 39

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/www/searchyouringredients.100webspace.net/checklogin.php:9) in /home/www/searchyouringredients.100webspace.net/checklogin.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/www/searchyouringredients.100webspace.net/checklogin.php:9) in /home/www/searchyouringredients.100webspace.net/checklogin.php on line 41

If someone could inform me what im doing wrong, so i dont make the same mistakes.. Thanks alot in advance! :banghead:

Re: Header Problems/Session Register

Posted: Thu Oct 23, 2008 4:54 pm
by Syntac
Make sure your script isn't sending output before it sets headers/cookies/etc.

Re: Header Problems/Session Register

Posted: Thu Oct 23, 2008 5:02 pm
by oscardog
I cannot see any output, the code is there, can someone let me know if they see output... or just post code corrections that will fix the problem.

Thanks

Re: Header Problems/Session Register

Posted: Thu Oct 23, 2008 5:07 pm
by Syntac
Is another file including this file?

Re: Header Problems/Session Register

Posted: Thu Oct 23, 2008 5:10 pm
by oscardog
Nope. If it were to all work, it would then go to login_success.php

Which has:

Code: Select all

 
<? 
session_start();
if(!session_is_registered(myusername)){
header("location:members.php");
}
?>
 
So, in answer to your question, no. But on members.php there is an include, but i doubt that has any bearing.

Re: Header Problems/Session Register

Posted: Fri Oct 24, 2008 7:08 am
by oscardog
Anyone? Really need help with this...

Re: Header Problems/Session Register

Posted: Fri Oct 24, 2008 12:49 pm
by TheBrandon
Your code has a line break before the <?php.

Try removing that output and see if it works.

Re: Header Problems/Session Register

Posted: Sat Oct 25, 2008 12:09 pm
by oscardog
There is no line break, its just i left a line before the code started on the forums so it looks like a line break.

Still not working... Need help :(

Re: Header Problems/Session Register

Posted: Sat Oct 25, 2008 3:10 pm
by oscardog
Seriously, this is probably one of the more simple problems i've seen on here and no-one can help?

Please?