Web Analytics Made Easy -
StatCounter Redirect on Error - CodingForum

Announcement

Collapse
No announcement yet.

Redirect on Error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Redirect on Error

    Can someone advise me on how I need to modify my PHP script.

    I have resolved my issues with my hosting company so the script works; this
    loads a teams fixtures from two CSV Files (Team Details from Section and Team Fixtures from the Fixture List). However I would like to add an error check where if the csv file for the Division or Team does not exist it diverts to an error page in case there are any errors (link typos etc).

    Currently I have the exit function to achieve this, which works but is not ideal (it doesn't display the page footer).

    I would like to divert if either the Division or Team files do not exist.

    My current code is

    PHP Code:
    <?php
    $div 
    $_GET["div"];
    $team $_GET["team"];
    $localTime=time();
    $localTime=$localTime - (60 60 1);
    $thisDay date("d",$localTime);
    $thisMonth date("m",$localTime);
    $thisYear date("Y",$localTime);
    $embold='N';

    if (
    $div == 'M'){
    $divCsv 'csv/dSectionM.csv';
    $divName 'Mixed League';
    }
    elseif (
    $div == 'Mixed'){
    $divCsv 'csv/dSectionMixed.csv';
    $divName 'Mixed League';
    }
    elseif (
    strlen($div) == 1){
    $divCsv 'csv/dSection'.$div.'.csv';
    $divName 'Section '.$div;
    }
    else {
    $divCsv 'csv/dLadiesSection'.substr($div,1,1).'.csv';
    $divName 'Ladies Section '.substr($div,1,1);
    }

    @
    $pageTitle $nameH ;
    include (
    'pageheader.php');
    $fCnt=0;
    // process list file
    $fd = @fopen ("$divCsv""r") or exit ("<div align=center><font face=arial size=2>Section Not Found, Not Available<br>
    or incorrectly entered<br>Please Contact the System Administrator<br></div><hr width=75% align=center color=green>"
    );
    // initialize a loop to go through each line of the file
    while (!feof ($fd)) {
    // declare an array to hold all of the contents of each row, indexed
    $buffer fgetcsv($fd4096);
    // the 8 represents the number of columns in the text file
    for ($i 0$i 8; ++$i){
    if (
    $i == 0){ $id $buffer[$i]; }
    if (
    $i == 1){ $name $buffer[$i]; }
    if (
    $i == 2){ $alley $buffer[$i]; }
    if (
    $i == 3){ $day $buffer[$i]; }
    if (
    $i == 4){ $captain $buffer[$i]; }
    if (
    $i == 5){ $phone $buffer[$i]; }
    if (
    $i == 6){ $tcsv $buffer[$i]; }
    if (
    $i == 7){ $www $buffer[$i]; }
    }
    if (
    $team == $tcsv){
    $nameH $name;
    $alleyH $alley;
    $dayH $day;
    $captainH $captain;
    $phoneH $phone;
    $wwwH $www;
    }
    }
    echo 
    "<table>\n";
    echo 
    "<tr>\n";
    echo 
    "<td valign=\"TOP\">\n";
    echo 
    "<table class=\"BORDER\">\n";
    fclose ($fd);
    $mmOld='';
    $csvFile 'csv/t'.$team.'.csv';
    // process division file
    $fd = @fopen ("$csvFile""r") or exit ("<div align=center><font face=arial size=2>The Team List You have Selected is either Not Available<br>
    or incorrectly entered<br>Please Contact the System Administrator</div>"
    );
    // initialize a loop to go through each line of the file
    while (!feof ($fd)) {
    // declare an array to hold all of the contents of each row, indexed
    $buffer fgetcsv($fd4096);
    // the 6 represents the number of columns in the text file
    for ($i 0$i 6; ++$i){
    if (
    $i == 0){
    $tweek $buffer[$i];
    }
    if (
    $i == 1){
    $tdate $buffer[$i];
    @list(
    $fDay$fMonth$fYear) = explode(" "$tdate);
    $chkMonth $fMonth;
    $date_formatted "$fDay $fMonth $fYear";
    $final_date strtotime($date_formatted);
    $fDay date("d",$final_date);
    $fMonth date("m",$final_date);
    $fYear date("Y",$final_date);
    if (
    $fYear == $thisYear $fMonth == $thisMonth $fDay == $thisDay $embold=='N'){
    $embold 'Y';
    }
    elseif (
    $fYear == $thisYear $fMonth == $thisMonth $fDay >= $thisDay $embold=='N'){
    $embold 'Y';
    }
    elseif (
    $fYear >= $thisYear $fMonth $thisMonth $embold=='N'){
    $embold 'Y';
    }
    elseif (
    $fYear $thisYear $embold=='N'){
    $embold 'Y';
    }
    }
    if (
    $i == 2){ $tday $buffer[$i]; }
    if (
    $i == 3){ $tversus $buffer[$i]; }
    if (
    $i == 4){ $thora $buffer[$i]; }
    if (
    $i == 5){ $talley $buffer[$i]; }
    }
    ++
    $fCnt;
    if (
    $fCnt == 1){
    echo 
    "<tr>\n";
    echo 
    " <td NOWRAP class=\"HDR\" colspan=\"6\"><b>$nameH</b></td>\n";
    echo 
    "</tr>\n";
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\" colspan=\"6\">\n";
    echo 
    "<b>$divName</b>\n";
    echo 
    "<br>Captain: <b>$captainH</b>\n";
    if (
    $phoneH != ''){
    echo 
    "<br>Tel: <b>$phoneH</b>\n";
    }
    $alleyAbbrev preg_replace("/[^a-zA-Z0-9]/"""$alleyH);
    echo 
    "  <br>Home: <b>$alleyH$dayH</b>\n";
    echo 
    " </td>\n";
    echo 
    "</tr>\n";
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">Week</td>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">Date</td>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">Day</td>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">Versus</td>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">H/A</td>\n";
    echo 
    "<td NOWRAP class=\"TITLE\">Alley</td>\n";
    echo 
    "</tr>\n";
    }
    if (
    $chkMonth == 'Jan' and $mmOld == 'Dec'){
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP colspan=\"6\" class=\"TITLECENTER\"><img src=\"blank.gif\"></td>\n";
    echo 
    "</tr>\n";
    }
    if (
    $tweek != '' $embold != 'Y'){
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"NORMALCENTER\">$tweek</td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\">$tdate</td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\">$tday</td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\">$tversus</td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\">$thora</td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\">$talley</td>\n";
    echo 
    "</tr>\n";
    }
    if (
    $tweek != '' $embold == 'Y'){
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"NORMALCENTER\"><b>$tweek</b></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\"><b>$tdate</b></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\"><b>$tday</b></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\"><b>$tversus</b></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\"><b>$thora</b></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\"><b>$talley</b></td>\n";
    $embold='X';
    echo 
    "</tr>\n";
    }
    $mmOld $chkMonth;
    }
    fclose ($fd);
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"TITLE\" colspan=\"6\"><img src=\"blank.gif\"></td>\n";
    echo 
    "</tr>\n";
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"NORMALCENTER\"><a href=\"csv/t$team.csv\" title=\"Download fixtures for $name in a csv file: Right-click and Save as...\" target=\"_blank\"><img border=0 SRC=\"excel.gif\"></a></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\" colspan=\"5\"><a href=\"csv/t$team.csv\" title=\"Download fixtures for $name in a csv file: Right-click and Save as...\" target=\"_blank\">download fixtures (in csv format)</a></td>\n";
    echo 
    "</tr>\n";
    echo 
    "<tr>\n";
    echo 
    "<td NOWRAP class=\"NORMALCENTER\"><a href=\"teamprint.php?team=$team&name=$nameH&div=$divName\" title=\"Opens a new page for printing\" target=\"_blank\"><img border=0 SRC=\"print.gif\"></a></td>\n";
    echo 
    "<td NOWRAP class=\"NORMAL\" colspan=\"5\"><a href=\"teamprint.php?team=$team&name=$nameH&div=$divName\" title=\"Opens a new page for printing\" target=\"_blank\">printable version (opens in a new window/tab)</a></td>\n";
    echo 
    "</tr>\n";
    echo 
    "</table>\n";
    echo 
    "</td>\n";
    echo 
    "<td>&nbsp;\n";
    echo 
    "</td>\n";
    echo 
    "<td valign=\"TOP\">\n";
    echo 
    "<table class=\"BORDER\" align=\"CENTER\">\n";
    echo 
    "<!--  on BDSL site, current division table goes here -->";
    echo 
    "</table>\n";
    $csvFile 'csv/listteam.csv';
    $fCnt=0;
    // process csv file
    $fd fopen ("$csvFile""r");
    // initialize a loop to go through each line of the file
    while (!feof ($fd)) {
    // declare an array to hold all of the contents of each row, indexed
    $buffer fgetcsv($fd1024);
    // the 5 represents the number of columns in the text file
    for ($i 0$i 5; ++$i){
    if (
    $i == 0){
    $tName $buffer[$i];
    $tAbbrevName preg_replace("/[^a-zA-Z0-9]/"""$tName);
    }
    if (
    $i == 4){
    @
    $tHist $buffer[$i];
    }
    }
    ++
    $fCnt;
    if (
    $tName != '' $tAbbrevName == $team $tHist != ''){
    echo 
    " ";
    }
    }
    fclose ($fd);
    if (
    $wwwH != ''){
    echo 
    "<p class=\"NORMAL\"><br><a href=\"$wwwH\" target=\"_blank\" title=\"$nameH web site\"><img src=\"resource/www.gif\" alt=\"$nameH web site\" border=\"0\"> $nameH website</a></p>\n";
    }
    echo 
    "</td>\n";
    echo 
    "</tr>\n";
    echo 
    "</table>\n";
    ?>
    <br>
    <hr width="75%" align="center" color="green">
    <br>
    </td>
    </tr>
    </table>
    <?php include ('pagefooter.php');?>
    I believe this can be done using the code

    PHP Code:
    <? php
    // do GET div, team etc
    // assign $divCsv
    // use ! before function to say NOT 
    if (!file_exists($divCsv)){
    header('Location: /error.html');
    }
    ?>

    and

    <? php
    // do GET team
    // assign $csvFile
    // use ! before function to say NOT 
    if (!file_exists($csvFile)){
    header('Location: /error.html');
    }
    ?>
    But I can't get it to work.

    Is this correc? If so where should this be placed in the code

    Any advice appreciated

  • #2
    I don't want to go through all of this first thing in the morning.
    Header will half work. First, header must always be followed by an exit/die call otherwise it will continue to process the script. If you have any resources open, they should be closed before moving to the exit. Next, header rfc officially states that the path should be absolute and not relative, though I've never encountered an error myself from browsers rejecting it.
    So the likely problem is where you are using it. Headers cannot be sent if they have already been sent and the browser is flushing info. Enable your error reporting:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    Originally posted by gazza3665 View Post

    If so where should this be placed in the code
    Wherever you're current exits are.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      thanks for your suggestion, I'm a little confused however at what I need to do. I'm not an expert on PHP, only really started learning. The script was written by someone else, I'm making minor modifications.


      How can I add the header () function into the Script?

      Comment

      Working...
      X