PHP Redirect script assistance

This is a section from a working logon script. We want to redirect to different URL’s based on the text in an existing database field. Don’t get any error msgs, jus blank page. Thanks for helping a rank amateur!

[php]

<?php session_start(); if(!session_is_registered(email)){ header("location:johnlogin1.php"); } if ($user->logged_in) { $query = "SELECT * FROM orders"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['description']; } if($description=='monkey') // condition for one keyword { $url='http://www.monkey.com'; // url or page to redirect on keyword header('Location:$url'); } if($description=='banana') // condition for other keyword { $url='www.banana.com'; header('Location:$url'); } ?>

[/php]

Are you sure? $description is undefined

Revised code to this version and checked for errors = none. Still no redirect - blank page…

[php]

<?php include 'johnlogin0.php'; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); { $sql="SELECT * FROM $tbl_name WHERE description='description'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['description']; } if(description=='monkey') // condition for one keyword { //$url='www.monkey.com'; // url or page to redirect on keyword header('Location:$url'); } if(description=='banana') // condition for other keyword { //$url='www.banana.com'; header('Location:$url'); } ?>

[/php]

That code is still invalid. In that case ‘description’ would be assumed a constant which is not defined.

Before I tell you the problem, you need to make sure you have proper error reporting.

Add this to the top of your script:

[php]error_reporting(E_ALL);[/php]

You will also need to verify that display_errors = On. If you don’t know how to check the php.ini you can do this:

[php]echo ini_get(‘display_errors’);[/php]

It should echo “1”

Script updated as below. No errors reported. No echo… Still blank page on execute.

[php]

<?php error_reporting(E_ALL); echo ini_get('display_errors'); include 'johnlogin0.php'; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); { $sql="SELECT * FROM $tbl_name WHERE description='description'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['description']; } if(description=='monkey') // condition for one keyword { //$url='www.monkey.com'; // url or page to redirect on keyword header('Location:$url'); } if(description=='banana') // condition for other keyword { //$url='www.banana.com'; header('Location:$url'); } ?>

[/php]

[php]echo ini_get(‘display_errors’);[/php]

Certainly wouldn’t echo a “blank page”

You most likey have log_errors = On and display_errors = Off. This causes a 500 internal server error and displays a blank page.

Log_error = On

Display_errors = On

Example posted in Item #4 above incorrect - here’s the right example - still displays blank page only an no errors.

[php]

<?php error_reporting(E_ALL); ini_set('display_errors', '1'); include 'johnlogin0.php'; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); { $sql="SELECT * FROM $tbl_name WHERE description='description'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['description']; } if(description=='monkey') // condition for one keyword { $url='www.monkey.com'; // url or page to redirect on keyword header('Location:$url'); } if(description=='banana') // condition for other keyword { $url='www.banana.com'; header('Location:$url'); } ?>

[/php]

There are a few errors in there so I don’t know why you are getting a blank page. Getting errors to display properly should be your priority.

Otherwise I’ll point out a couple errors.

[php]$sql=“SELECT * FROM $tbl_name WHERE description=‘description’”;[/php]

$tbl_name is undefined from what I can see. Also, description=‘description’ condition would prevent any possibility of description=‘monkey’

[php]if(description==‘monkey’)[/php]

This would be seen as an (undefined) constant. It should be $row[‘description’]

[php]
$url=‘www.monkey.com’;
header(‘Location:$url’);[/php]

This is invalid without the http:// in the url. Otherwise it would redirect to http://www.domain.com/www.monkey.com

Thanks for the suggestions.

$tbl_name is defined in the “include” statement.

Error reporting is on - no responses.

Other suggestions included, as shown below. No Joy.

[php]

<?php error_reporting(E_ALL); ini_set('display_errors', '1'); include 'johnlogin0.php'; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); { $sql="SELECT * FROM $tbl_name WHERE description='description'"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['description']; } if($row['description']=='monkey') // condition for one keyword { $url='http://www.monkey.com'; // url or page to redirect on keyword header('Location:$url'); } if($row['description']=='banana') // condition for other keyword { $url='http://www.banana.com'; header('Location:$url'); } ?>

[/php]

Sorry I cannot offer much help but maybe I can suggest watching this tutorial on youtube. I cannot post urls so search for phpacademy redirect_to and it is the first tutorial from ‘the newboston’.

Sponsor our Newsletter | Privacy Policy | Terms of Service