strange...

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

Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

strange...

Post by Ne0 »

ok, im a newbie to PHP but ive finally got it down.
before i ask my question, ill give u some background on what im doing
i have a site (duh) and i have a header.php file, the accual page, and then a footer.php file. I built a login/register system off a tutorial, and it all works fine, but

in the login page, everything works perfect until i accually include the header file, so like:
this works:

Code: Select all

<? 
//include 'header.php';
/* Check User Script */ 
$user="ne0";
$password2="****";
$database="db1";
mysql_connect(localhost,$user,$password2);
@mysql_select_db($database) or die("Unable to select database");


// Conver to simple variables 
$username = $_POST['username']; 
$password = $_POST['password']; 

if((!$username) || (!$password)){ 
    echo "Please enter ALL of the information! <br />"; 
    include 'login_form.html'; 
    exit(); 
} 

// Convert password to md5 hash 
$password = md5($password); 

// check if the user info validates the db 
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'"); 
$login_check = mysql_num_rows($sql); 

if($login_check > 0){ 
    while($row = mysql_fetch_array($sql)){ 
    foreach( $row AS $key => $val ){ 
        $$key = stripslashes( $val ); 
    } 
        // Register some session variables! 
        session_register('first_name'); 
        $_SESSION['first_name'] = $username; 

        session_register('email_address'); 
        $_SESSION['email_address'] = $email_address; 
        session_register('special_user'); 
        $_SESSION['user_level'] = $user_level; 
         
        mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'"); 
         
        header("Location: login_success.php"); 
    } 
} else { 
    echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br /> 
    Please try again!<br />"; 
    include 'login_form.html'; 
} 
?>
but when i get rid of the // b4 the include 'header.php'; i go to login, everything works fine, and then when i submit it it brings me to a page with just the header, and nothing else.

please help if u can, thanks
-Ne0
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: strange...

Post by TheBentinel.com »

Ne0 wrote:

Code: Select all

<? 
//include 'header.php';
...
        header("Location: login_success.php"); 
[/quote]

It looks like you're trying to set a header after you've already sent some html to the browser (in the header.php file)

If you're going to redirect them in this file, then you probably want to move that "include header" stuff lower in the logic, so it only happens when you aren't trying to send them to login_success.php

Does that make sense?
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

bingo, it worked perfectly thank you.

i understand what i had to do, but i dont understand why. Normally the problems i have people have answered that their has already HTML been sent to the browser or something like that, can someone explain why PHP does like this and how to avoid it?

thanks again
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Ne0 wrote:bingo, it worked perfectly thank you.

i understand what i had to do, but i dont understand why.
It's not really PHP, it's the HTTP protocol. The same problem (and same-sounding error) comes up in ASP.

In a communication from the server to the browser, there is a header, looking something like this:
Content-Length: 1500
Location: http://RedirectTo.com

This is followed by a blank line, then the content starts up:
<html>
<head>....

Once you've output some text, PHP closes off the headers and starts sending the content. After you've already sent content, it's too late to go back and send a header.

Cookies and redirects are sent in the header, so you can't drop a cookie or try to redirect after content has already been sent.

I understand the need to know "why". Does that explain it, or did I fudge it too badly?
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

no, that makes alot of sense now that i see it. but now i think im having the same problem (mabey not but eh..the more i see how to fix them the more i will get used to it)

Code: Select all

<?
if (!$_SESSION['first_name']){
	echo "&nbsp&nbsp ( <a href=login.php>Log In</a> )( <a href=index.asp?p=form&f=reg>Register</a> )";
}else{
	echo "&nbsp&nbsp ( <a href=cp.php>Control Panel</a> )( <a href=logout.php>Logout</a> )( <font color=gray>$_SESSION['first_name']</font> )";
}
?>
i put that in and it seemed to cancel out the header.php completly out.

btw thanks for you help :)
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Ne0 wrote:

Code: Select all

<?
if (!$_SESSION['first_name']){
	echo "&nbsp&nbsp ( <a href=login.php>Log In</a> )( <a href=index.asp?p=form&f=reg>Register</a> )";
Is that really supposed to be index.asp? or should it be index.php?

What error are you getting?
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

thats in the header.php, and its not giving an error, its just acting like header.php doesnt even exists (like its not including it)
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Ne0 wrote:thats in the header.php, and its not giving an error, its just acting like header.php doesnt even exists (like its not including it)
Are you getting any output at all? What does view source show?
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

im getting the page, and whats in the footer. in other words everything but the entire header.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Ne0 wrote:im getting the page, and whats in the footer. in other words everything but the entire header.
What's in the header? Can you post the code?

If you take out all the code and just put in a:
print "I'm here";

do you see the message?
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

yes, that shows. infact the whole thing worked fine b4 i added that code with the Session stuff...

Code: Select all

<?
$img_dir = "imgs"
?>

<html>
</head>
<title>Ne0's GameMaker Portal</title>
<style>
a&#123;
color:gray;&#125;
a:hover&#123;
color:0055CC;&#125;
</style>
</head>
<body background="<? echo $img_dir ?>/bg.gif">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="740" height="500">
</center>
 <tr>
  <td valign="top" colspan="3">
	<img src="<? echo $img_dir ?>/header_01.jpg">
  </td>
 </tr>
 <tr>
  <td style="background-image: url(<? echo $img_dir ?>/header_0202.jpg);" height="22" valign="top" colspan="3">
	<b>
<?
if (!$_SESSION&#1111;'first_name'])&#123;
	echo "&nbsp&nbsp ( <a href=login.php>Log In</a> )( <a href=index.asp?p=form&f=reg>Register</a> )";
&#125;else&#123;
	echo "&nbsp&nbsp ( <a href=cp.php>Control Panel</a> )( <a href=logout.php>Logout</a> )( <font color=gray>$_SESSION&#1111;'first_name']</font> )";
&#125;
?>
  </td>
 </tr>
 <tr>
  <td valign="top" height="100%" width="120">


	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(<? echo $img_dir ?>/top.gif);">
	&nbsp<font color="white"><b>Menu</td></tr><tr><td bgcolor="white" align="center">
	<a href="index.php?p=home">Home</a><br>
	<a href="index.php?p=games">Games</a><br>
	<a href="index.php?p=examples">Examples</a><br>
	<a href="index.php?p=sprites">Sprites</a><br>
	<a href="index.php?p=3d">3D Models</a><br>
	<a href="forum/index.php?a=indx">Forum</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->
<br>
	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(<? echo $img_dir ?>/top.gif);">
	&nbsp<font color="white"><b>User Info</td></tr><tr><td bgcolor="white" align="center">
	<a href="">Edit Profile</a><br>
	<a href="">View Stats</a><br>
	<a href="">Change Password</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->
	<br>
	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(<? echo $img_dir ?>/top.gif);">
	&nbsp<font color="white"><b>Submit</td></tr><tr><td bgcolor="white" align="center">
	<a href="">Submit File</a><br>
	<a href="">Submit News</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->


  </td>
  <td width="500" valign="top" height="100%">


	<!-- -- -- -- -- -- -- -- Main Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" bgcolor="0060FF" style="background-image: url(<? echo $img_dir ?>/top.gif); background-repeat: no-repeat;">
	&nbsp<font color="White"><b>Content</td></tr><tr><td bgcolor="white">
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Ne0 wrote:yes, that shows. infact the whole thing worked fine b4 i added that code with the Session stuff...

Code: Select all

<?
$img_dir = "imgs"
?>
Stuff like this should end with a semi-colon. I'd change it, but I doubt that's where your problem is.

If you take out the session stuff you mention, does the problem go away?

When I'm faced with something like this, I take out all the PHP code and see if just the HTML will show up. Then start adding in the PHP code a block at a time, until it breaks. That narrows it down.
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

ok, i took out every single line of php in the header file, and it worked fine. I changed all of the $img_dir stuff just to plain old "imgs/img.gif" and it all worked perfectly fine. Then, i added this:

Code: Select all

<?
if (!$_SESSION['first_name']){
	echo "&nbsp&nbsp ( <a href=login.php>Log In</a> )( <a href=index.asp?p=form&f=reg>Register</a> )";
}else{
	echo "&nbsp&nbsp ( <a href=cp.php>Control Panel</a> )( <a href=logout.php>Logout</a> )( <font color=gray>$_SESSION['first_name']</font> )";
}
?>
and the whole thing acted like their was no header, like it wasnt being included at all. In View->Source their wasnt a single line of anything from the header. But the rest was their.

o and heres what i have now: header.php:

Code: Select all

<html>
</head>
<title>Ne0's GameMaker Portal</title>
<style>
a{
color:gray;}
a:hover{
color:0055CC;}
</style>
</head>
<body background="imgs/bg.gif">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="740" height="500">
</center>
 <tr>
  <td valign="top" colspan="3">
	<img src="imgs/header_01.jpg">
  </td>
 </tr>
 <tr>
  <td style="background-image: url(imgs/header_0202.jpg);" height="22" valign="top" colspan="3">
	<b>
<?
if (!$_SESSION['first_name']){
	echo "&nbsp&nbsp ( <a href=login.php>Log In</a> )( <a href=index.asp?p=form&f=reg>Register</a> )";
}else{
	echo "&nbsp&nbsp ( <a href=cp.php>Control Panel</a> )( <a href=logout.php>Logout</a> )( <font color=gray>$_SESSION['first_name']</font> )";
}
?>
  </td>
 </tr>
 <tr>
  <td valign="top" height="100%" width="120">


	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(imgs/top.gif);">
	&nbsp<font color="white"><b>Menu</td></tr><tr><td bgcolor="white" align="center">
	<a href="index.php?p=home">Home</a><br>
	<a href="index.php?p=games">Games</a><br>
	<a href="index.php?p=examples">Examples</a><br>
	<a href="index.php?p=sprites">Sprites</a><br>
	<a href="index.php?p=3d">3D Models</a><br>
	<a href="forum/index.php?a=indx">Forum</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->
<br>
	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(imgs/top.gif);">
	&nbsp<font color="white"><b>User Info</td></tr><tr><td bgcolor="white" align="center">
	<a href="">Edit Profile</a><br>
	<a href="">View Stats</a><br>
	<a href="">Change Password</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->
	<br>
	<!-- -- -- -- -- -- -- -- Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" style="background-image: url(imgs/top.gif);">
	&nbsp<font color="white"><b>Submit</td></tr><tr><td bgcolor="white" align="center">
	<a href="">Submit File</a><br>
	<a href="">Submit News</a><br>
	</td></tr></table>
	<!-- -- -- -- -- -- -- -- End Box -- -- -- -- -- -- -- -- -->


  </td>
  <td width="500" valign="top" height="100%">


	<!-- -- -- -- -- -- -- -- Main Content Box -- -- -- -- -- -- -- -- -->
	<table width="100%"><tr><td height="20" bgcolor="0060FF" style="background-image: url(imgs/top.gif); background-repeat: no-repeat;">
	&nbsp<font color="White"><b>Content</td></tr><tr><td bgcolor="white">
Ne0
Forum Commoner
Posts: 60
Joined: Sat Feb 14, 2004 11:48 am

Post by Ne0 »

please, someone, i dunno why this small code cancles out the whole header file
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

regarding sessions: do you have cookies enabled in your browser?
Post Reply