SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change Header Redirect based on Error Conditions

    Hello

    When developing an application, exceptions (errors) can occur. I noticed that using the header( “Location” directToPage.php ), redirects to the page even if errors occur. I also noticed that it has to be the first line in the php code.

    So I am curious, how do others use redirect logic when exceptions (errors) occur? For example:
    Code:
    <?php
    	//-- I have to put the redirect here for it to work
    	header( “Location” successPage.php );
    
    	//-- I use a Data Access object to insert data but the insert fails
    	//-- so instead of directing to successPage.php, I need to direct to errorPage.php
    	$dao = new InsertDAO();
    	If( $dao.insert() ) {
    		//-- successful insert, let successPage.php redirect occur
    	} else {
    		//-- here is where the failed insert is caught… I now want to direct to
    		//-- the errorPage.php but I am not sure how I can… this does not work
    		header( “Location” errorPage.php );
    	}
    Any feedback is appreciated.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    for one, your syntax is incorrect.
    SB
    PHP Code:
    header"Location: errorPage.php" ) ;
    exit();  
    // to be sure 

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The location header call doesn't have to be first, it just has to be before any output. So feel free to process any amount of logic prior, just don't output anything.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for the clarification. Understanding that does allow logic to be applied then. Just to make sure I completely understand output… output is considered sending a response or flushing a stream but it is not considered writing to a file. Is this correct?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Correct.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, http://www.php.net/manual/en/book.outcontrol.php

    Sometimes, it may be more convenient to let help with certain buffering tasks.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for the feedback.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •