PHP Catastrophe! header() hates me.

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
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

PHP Catastrophe! header() hates me.

Post by ast3r3x »

...it rhymes, it can't be good!

My lovely site was/is working fine on my computer. However, I upload it to the server space someone was kind enough to buy for me, and it goes to hell. Basically, my redirection with header() stopped working. It's not that the header() function doesn't work, it's that I seem to not be able to have ANY code above of it.

Now I know I know, you are going to say, "yes, it says you can't do that in the manual", but I mean php code. Like a test to whether it should do it or not.

My phpinfo()
Their phpinfo()

Mostly, I'm just including the code code before my header() redirects, but I'm not outputting anything first, which should (to my knowledge) not effects the outcome then, because I didn't start the page yet.

Basically, I need help bad, I'll give you any code you want to look at, just let me know.

Edit: I'm not using absolute URL's, as in header("Location: ../login.php"), but I tried the full URL and it also didn't work. Plus the partial also worked on my computer.

I'm using sessions if that matters. I tried closing them first, but it didn't help.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Post everything before you call header("Location.blah
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

I use header() all over the place, so here is an example of when it fails trying to log in...

I send the username (uid) and password (pwd) to login.php:

Code: Select all

<?php

require_once("CheckLP.php");

if(session_id() == NULL)
{
	session_start();
}
?>

<?php

$error = false;
$uiderror = 0;
$pwderror = 0;

if(!isset($_POST['uid']) || isset($_POST['uid']) == "")
{
	//die("uid");
	$uiderror = 1;
	$error = true;
}

if(!isset($_POST['pwd']) || isset($_POST['pwd']) == "")
{
	$pwderror = 1;
	$error = true;
}

if($error == true)
{
	$referer = $_SERVER['HTTP_REFERER'];
	header("Location: ../login.php");
	//die("before");
	exit;
	//die("after");
}


$uid = $_POST['uid'];
$pwd = $_POST['pwd'];

if(CheckLP($uid, $pwd))
{
	session_register('uid');
	$_SESSION['uid'] = $uid;
	
	session_register('logged');
	$_SESSION['logged'] = true;
	
	session_register('uid');
	$_SESSION['uid'] = $uid;
	
	session_register('style');
	$userdata = GetData("users", "style", "uid", $uid);
	if($userdata != NULL)
	{
		$_SESSION['style'] = $userdata[0];
	}
	else
	{
		$_SESSION['style'] = "blue";
	}
	
	session_unregister('lperror');
	
	header("Location: ../logged.php");
	exit;
}
else
{
	//die("dead");
	session_register('logged');
	$_SESSION['logged'] = false;
	
	session_register('lperror');
	$_SESSION['lperror'] = true;
}

$referer = $_SERVER['HTTP_REFERER'];
header("Location: ../login.php");
exit;

?>
As you can see that includes a file with the function to check my passwords:

Code: Select all

<?php include("../insert/intouchdb.html"); ?>

<?php

function CheckLP($uid, $pwd)
{
	$userdata = GetData("users", "pwd", "uid", $uid);
	$pwdmd5 = md5($pwd);
	if($userdata[0] != NULL && $pwdmd5 == $userdata[0])
	{
		return true;
	}
	else
	{
		return false;
	}
}

?>
That, finally includes the functions I use to connect to my database:

Code: Select all

<?php

#########################
## Prerequisite for DB ##
## ######################
require_once('DB.php');
global $db;
$db = DB::connect("mysql://dbo107486618:qfpWZ6jt@db108.perfora.net/db107486618");
if(DB::isError($db))
{
	//die("isError1");
	die($db->getMessage());
}

//Return An Array of data in 
function GetData($table, $querydata, $whereitem = NULL, $wherevalue = NULL, $sortby = NULL)
{
	//die(var_dump($where));
	$db = &$GLOBALS['db'];
	$sql = "SELECT {$querydata} FROM {$table}";
	if($whereitem != NULL && $wherevalue != NULL)
	{
		$where = " WHERE $whereitem="$wherevalue"";
		$sql = $sql.$where;
		//die(var_dump($sql));
	}
	
	if($sortby != NULL)
	{
		$sortby = " ORDER BY $sortby";
		$sql = $sql.$sortby;
		//die(var_dump($sql));
	}
	
	//die(var_dump($sql));
	$query = $db->query($sql);
	if(DB::isError($query))
	{
		die($query->getMessage());
	}
	
	while($query->fetchInto($row))
	{
		foreach($row as $data)
		{
			$dbdata[] = $data;
		}
	}
	
	if(isset($dbdata) && $dbdata != NULL)
	{
		return $dbdata;
	}
}

function InsertData($table, $columns, $values)
{
	$count = count($columns);
	
	$cols = "$columns[0]";
	$vals = ""$values[0]"";
	
	for($x = 1; $x < $count; $x++)
	{
		$cols = "{$cols}, {$columns[$x]}";
		$vals = "{$vals}, "{$values[$x]}"";
	}
	
	$sql = "INSERT INTO {$table} ({$cols}) VALUES ({$vals})";
	
	//die($sql);
	
	$db = &$GLOBALS['db'];
	$query = $db->query($sql);
	if(DB::isError($query))
	{
		die($query->getMessage());
	}
	
	$referer = $_SERVER['HTTP_REFERER'];
	header("Location: {$referer}");
}

function UpdateData($table, $column, $value, $whereitem = NULL, $symbol = NULL, $wherevalue = NULL, $string = NULL)
{
	if($string == true)
	{
		$quote = """;
	}
	
	$sql = "UPDATE {$table} SET {$column}={$quote}{$value}{$quote} WHERE {$whereitem}{$symbol}"{$wherevalue}"";
	//var_dump($sql);
	
	
	$db = &$GLOBALS['db'];
	$query = $db->query($sql);
	if(DB::isError($query))
	{
		die($query->getMessage());
	}
	
	return $sql;
}



?>
That is the code as it was, working on my computer (OS X, with the entropy.ch PHP whatever). As you can see I still have a descent amount of test code still in :oops: :P


It seems so simple when I do it in my pages, but when I have to put it all together like that to show what it has to do, seems kinda complicated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

&#123;
   session_start();
&#125;
?>

<?php

$error = false;
$uiderror = 0;
that'd be the problem... :lol:

(remove the jumping in and out of php)
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Sorry...what would be the problem?

Does jumping in and out make possible errors or what?

The problem I seem to be having is that it just ignores the header(), gets to the exit and of course stops.
Last edited by ast3r3x on Thu Aug 19, 2004 9:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the gap between them outputs some text (whitespace) that sends the headers.. :)
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Wow. That is really stupid, but it seems like it's working. Why would it not do it on my computer, but it would on their server?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sometimes, if the script is run fast enough and the connection is wide open (fast), some text output just before a call that outputs some headers will get buffered behind the headers.. it's kinda odd.. but I've had it happen..
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

I guess my next question I'll try to stick in this thread...

When I login, I start and make the session variables I need. I have them, and checked before I used header() to go to the next page that tells me I have logged in. When I get to that page, my session information no longer exists. What happened?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

You need to make sure that you call [php_man]session_start[/php_man]() on every page that uses the $_SESSION variables.
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Well it's working! I seem to have my work ahead of me, adding the necessities and getting rid of unnecessary code. Thanks for the help everyone! I guess I found a new forum I need to frequent :)
Post Reply