PHP die redirect

I have a centralized error page that I would like all die/failures to call for a consistent look to a site under development.

The error message I am receiving is:
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

The index page begins with the code listed.  I have also cut and paste the top of the required file.

Thanks in advance for any help.
index.php:
<?php 
if(!isset($_SESSION))
{
	session_set_cookie_params('600'); // 10 minutes.
	session_start();
}

// BRING IN OUR COMMON CODE
require_once('ds-common.php');

?>

ds-common.php:

<?php
session_start();

// set CommunityId
$_SESSION["CommunityId"] = 8388607;
$_SESSION["DBlink"] = 'localhost';
$_SESSION["DBuser"] = 'DB';
$_SESSION["DBpw"] = 'pw';
$_SESSION["DBname"] = 'DBname';

// Database connection
   $link = mysql_connect($_SESSION["DBlink"], $_SESSION["DBuser"], $_SESSION["DBpw"]) or die(header("Location: ./incomplete.php"));

...
?>

Open in new window

kkbenjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I do not see any redirects in this code at all.  You may have a loop in your .htaccess file.
0
kkbenjAuthor Commented:
or die(header("Location: ./incomplete.php"));
0
Ray PaseurCommented:
Oh, sorry I missed that.  I have never seen anyone try to use the die() command that way.  Try something more like this for your DB connect and select.  It will give you helpful error messages if anything goes wrong.

If you want to redirect on a DB error, you might want to throw a meta-refresh tag into the browser output stream.  You can add a wait time to it, then you can see the error messages for a few moments before the screen is wiped away by the browser.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php



// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

kkbenjAuthor Commented:
I don't want more errors displayed.  I want all die's redirected to one common page for continuity.
0
Ray PaseurCommented:
As a general rule, I would say that if you need to use die() you do not have a production-ready system and therefore you really want to see the errors so you can correct them and not die().  But that aside, you can use the header("Location") and provide the location URL with a GET argument string.  You can set the error messages in the GET string and the common page can handle them in any way that makes sense for your business needs.  Or you can set the error messages in the $_SESSION array if you have some reason that you want to hide the messages.

Maybe something like this:

$link = mysql_connect($_SESSION["DBlink"], $_SESSION["DBuser"], $_SESSION["DBpw"]);
if (!$link)
{
    header("Location: ./incomplete.php?err=" . mysql_error() );
    exit;
}

The header("Location") will redirect the browser and the exit will kill the current script.

Just curious... Where did you get the idea of using die() and a redirect at the same time?  Was this from a class or a teaching example you found somewhere?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FugasCommented:
kkbenj, it seems, that you are using just one redirect. So check your apache configuration, if there are redirects allowed. It should be like this
<Directory "/path/to/my_app">
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    allow from all
</Directory>
0
kkbenjAuthor Commented:
Your explanation makes sense.  I will alter what I was trying to your code.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.