How can I redirect a user?

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
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

How can I redirect a user?

Post by Seona »

Hi guys,

I'm trying to teach myself PHP, and I've spent a while looking for some code that will do a particular job, but haven't had any luck so far.

What I want is to be able to redirect a user to a different page. The logic I'm trying to use is:

if (condition is true)
send user to the page they wanted
else
send user to the error page


Is these a nice easy way to do this?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

if ($foo) {
    header('Location: http://example.org/page.php');
} else {
    header('Location: http://example.org/error.php');
}
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Great, that's exactly what I wanted. :)

Thanks.
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Hmm, I think I've found a limitation to this, or I'm doing something else wrong.

I have a processing page for a form. It includes another file which has some universal functions in it - I wanted to keep them seperate for modularity. So I include the file, do some basic validation of the data, put the data into the database, create a folder on the server based on what was in the form, and then bump the user off to the results page.

That's the theory, anyway. What I get is the following error:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/_fuctions/directoryManagement.php:58) in /var/www/html/adminAdd.php on line 29


Line 29 is, unsurprisingly, header('Location: http://onehouseproductions.com/siteAdmin.php');

Why doesn't it like it? Why does my other processing page, which I was working on yesterday when I first asked the question, understand this code perfectly?

I is confuzzled. :?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

php gives you the reason:

output started at /var/www/html/_fuctions/directoryManagement.php:5

rtfm [php_man]header[/php_man] and discover what your problem is.
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Hmm... OK, so apparently I have something outputting something before the redirect. The trouble is, I can't figure out what. The included file has three functions in it, but they shouldn't be outputting anything as far as I can tell. And I can't find anything else in my code that will be outputting anything either.

In case anyone more familiar with PHP than me (which is pretty much everyone here - I began working with it for the first time yesterday) can see what I'm missing, here's the two files.

The processing file:

Code: Select all

<?php
	include '_fuctions/directoryManagement.php';

	// declare some relevant variables
	$DBhost = "localhost";
	$DBuser = "myname";
	$DBpass = "mypass";
	$DBName = "mydb";
	$table = "client_site";
	// this converts the form variables somehow
	import_request_variables('p', 'p_');
	// connect to database
	$link = mysql_connect($DBhost,$DBuser,$DBpass);
	@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 	
	
	/**************************************/
	/*       check if client exists       */
	/**************************************/
	// create the query to compare the form data with the records in the client_site table
	$sqlquery = "SELECT * FROM $table WHERE clientName = '" . $p_clientName . "' OR sitePath = '" .  $p_sitePath . "'";
	// run the query and check it worked
	$result = mysql_query($sqlquery,$link);
	if (!$result = mysql_query($sqlquery,$link))
    	exit("Illegal query");
	// get the number of rows returned by the query
	$number = mysql_num_rows($result);
	// if query returned any records, abort
	if ($number != "0") {
		header('Location: http://onehouseproductions.com/siteAdmin.php');
		exit;
	}
	
	/*************************************/
	/*    add details to the database    */
	/*************************************/
	// create the query to add data to the client_site table
	$sqlquery = "INSERT INTO $table VALUES ('','" . $p_clientName . "','" .  $p_sitePath . "')";
	// run the query and check it worked
	$result = mysql_query($sqlquery,$link);
	if (!$result = mysql_query($sqlquery,$link))
    	exit("Illegal query");
	
	
	/*************************************/
	/*      create folder on server      */
	/*************************************/
	
	//is_emtpy_folder($p_sitePath);
	
	if (!Another_is_dir($p_sitePath)) {	
		FtpMkdir(mainwebsite_html,$p_sitePath);
	}
	header('Location: http://onehouseproductions.com/siteAdmin.php');

?>
And the included file with the functions:

Code: Select all

<?php
	/****************************************/
	/*        check if folder exists        */
	/****************************************/
	function Another_is_dir($file) {
		return ((fileperms("$file") & 0x4000) == 0x4000);
	}

	/***************************************/
	/* check if folder exists and is empty */
	/***************************************/
	function is_emtpy_folder($folder){
		if(is_dir($folder) ){
			$handle = opendir($folder);
			while( (gettype( $name = readdir($handle)) != "boolean")){
				$name_array[] = $name;
			}
			foreach($name_array as $temp)
			$folder_content .= $temp;
	
			closedir($handle);
			if($folder_content == "...") {
				return true;
			} else {
				return false;
			}
		}
		else
			return true; // folder doesnt exist
	}

	/*******************************************/
	/* create directory through FTP connection */
	/*******************************************/
	function FtpMkdir($path, $newDir) {
		$server='www.onehouseproductions.com'; // ftp server
		$connection = ftp_connect($server); // connection
		
		// login to ftp server
		$user = "myname";
		$pass = "mypass";
		$result = ftp_login($connection, $user, $pass);
	
		// check if connection was made
		if ((!$connection) || (!$result)) {
			return false;
			exit();
		} else {
			ftp_chdir($connection, $path); // go to destination dir
			if(ftp_mkdir($connection,$newDir)) { // create directory
				return $newDir;
			} else {
				return false;       
			}
			ftp_close($conn_id); // close connection
		}
	}

?>

The functions have all been written by other people and gleaned from various sources (mostly the user comments in the php.net manual) so I must confess to not being entirely certain what they do or why.

I would be very grateful if someone could look over my code and tell me why it hates me.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

look at the html source

whatever is before the error message php spits out, is your problem

blank lines and spaces etc.. are all considered output
Last edited by rehfeld on Thu Dec 09, 2004 7:51 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah if you have 1 space before the <?php tag, and possibly even after the ?> in an included file, that is considered output.
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Ah, that was what did it. A single space. Thanks guys.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

personally i like to use the handy of javascript to redirect.
u might give it a try.

Code: Select all

<script language="javascript">
		location.replace("page.php");
		</script>
Post Reply