Header Problems/Session Register

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Header Problems/Session Register

Post 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:
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Header Problems/Session Register

Post by Syntac »

Make sure your script isn't sending output before it sets headers/cookies/etc.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Header Problems/Session Register

Post 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
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Header Problems/Session Register

Post by Syntac »

Is another file including this file?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Header Problems/Session Register

Post 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.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Header Problems/Session Register

Post by oscardog »

Anyone? Really need help with this...
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Header Problems/Session Register

Post by TheBrandon »

Your code has a line break before the <?php.

Try removing that output and see if it works.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Header Problems/Session Register

Post 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 :(
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Header Problems/Session Register

Post by oscardog »

Seriously, this is probably one of the more simple problems i've seen on here and no-one can help?

Please?
Post Reply