Jump to content


Photo

header vs include


  • Please log in to reply
3 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 July 2006 - 07:48 AM

I am creating a members system and am using the switch statement in several loactions for instance my process.php. In this file I will have the login, register, sendmail and possibley others.

The problem that I am running into is that I want to also use switch statments for error and sucessful transactions processing.

Ok lets say that a user logs in and the post is sent to process.php?action=login. I have got that working fine but I want to redirect, if any errors, back to login.php?showerror=the_error and if the login is successful I want to send them to acount.php.

here is my login script:
<?php
session_start();

header("Cache-control: private");

$user = $_POST['username'];
$pass = $_POST['password'];

include("includes/db.php");

$sql_user_check = "SELECT username FROM users WHERE username='$user'";
$result_name_check = mysql_query($sql_user_check) or die(mysql_error());
$usersfound = mysql_num_rows($result_name_check) or die(mysql_error());

if($usersfound < 1){
	$error = "User ".$user." was not found in the database.";

		$user = $_POST['username'];
		$pass = md5($_POST['password']);
		$sql = "select * from users where `username` = '$user'";
		$result = mysql_query($sql);
		while ($text = mysql_fetch_array($result)) {
			$id = $text['id'];
			$password = $text['encrytpass'];
			$access = $text['access'];
		}
		if ($pass == $password) {
			$error = "Wrong Username / Password <a href=\"../index.php\">Back</a>"; //I want to referr the user back 
	                                                                                //to the login.php?showerror=the_error
	}else{
		$_SESSION['username'] = $user_info['username'];
	}
}

if(!$_SESSION['username']){
	if($error){
		echo $error;
		include("index.php");
	}else{
		echo "You are logged in.";
		include("../account.php");
	  }
    }else{
		echo "<html><head><title>Welcomce Back</title></head>Welcome back ". $_SESSION['username'] .".<a href=index.php>Click here</a> to proceed."; 
	}
?>

When I try to use header, instead of echo and include, I get the following error

Warning: Cannot modify header information - headers already sent by (output started at D:\home\www\owpt\public\home\includes\db.php:12) in D:\home\www\owpt\public\home\includes\process.php on line 142

Any suggestions would be great.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 indalecio

indalecio
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 05 July 2006 - 07:57 AM

Its because a header() function must be sent before ANY html gets output to the page.  That counts as even a blank space.  So if you ever close php and try to reopen php and send a header, wont work.  If you use echo or print before header ... it won't work.  If redirecting is that important to you, then do all of your user logging in before anything else hits the page, and try using a variable to set the Redirect through an if else statement.  If this variable is true, then run a redirect, otherwise no.

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 July 2006 - 08:28 AM

ok the main reason that I am wanting to do it this way is because when there is an error it includes the login.php and throws the allignment of my page off. I never thought about using an ifelse statement for the the include.

What I mean is that I could define the error as a variable in process.php and if that variable exists then it would pass the variable to the login.php.

or I could just include login.php?showerror=the_error. LoL.

Thanks for the advice. I looked in the manul and using header would over complicate this whole process.

See doing it that way I run into another problem. I am wanting to hide the process.php all to gether. I would rather the user to be sent to account.php with having ever seen the process.php.

Any suggestions on how I can do this?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 July 2006 - 08:44 AM

I got my thinking cap on and maybe a javascript would work better in this instance.

Something like this:

<script>
self.location = 'newpage.html';
</script>

the only thing is that I am not sure if I can pass the session variable this way or not. I will do some research on this issue and post the fix.

Thanks

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users