Avatar of kkbenj
kkbenj

asked on 

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

PHP

Avatar of undefined
Last Comment
kkbenj
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I do not see any redirects in this code at all.  You may have a loop in your .htaccess file.
Avatar of kkbenj
kkbenj

ASKER

or die(header("Location: ./incomplete.php"));
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

Avatar of kkbenj
kkbenj

ASKER

I don't want more errors displayed.  I want all die's redirected to one common page for continuity.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Fugas
Fugas
Flag of Slovakia image

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>
Avatar of kkbenj
kkbenj

ASKER

Your explanation makes sense.  I will alter what I was trying to your code.

Thanks!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo