[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP die redirect

Posted on 2011-10-09
7
Medium Priority
?
599 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:kkbenj
  • 3
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36939239
I do not see any redirects in this code at all.  You may have a loop in your .htaccess file.
0
 

Author Comment

by:kkbenj
ID: 36939313
or die(header("Location: ./incomplete.php"));
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36939362
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kkbenj
ID: 36939390
I don't want more errors displayed.  I want all die's redirected to one common page for continuity.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36940329
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
 
LVL 4

Expert Comment

by:Fugas
ID: 36952496
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
 

Author Closing Comment

by:kkbenj
ID: 36952526
Your explanation makes sense.  I will alter what I was trying to your code.

Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question