Jump to content

header vs include


tomfmason

Recommended Posts

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:
[code]<?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.";
}
?>[/code]

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

[code]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[/code]

Any suggestions would be great.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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

Something like this:

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

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.