How to redirect to another URL instead of generating error message?

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 13, 2017   (RSS)

By furcat - June 7, 2017

I have the following code in a file for one of my websites:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/rossinimanagement.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($case_studiesRecords, $case_studiesMetaData) = getRecords(array(
'tableName' => 'case_studies',
));

list($first_pageRecords, $first_pageMetaData) = getRecords(array(
'tableName' => 'first_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$first_pageRecord = @$first_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$first_pageRecord) { dieWith404("Record not found!"); }

?>

I want to direct the viewer to another page if there is an error and/or if the record is not found instead of generating an error message. Is that possible, and if so, how do I do it?

By Damon - June 7, 2017 - edited: June 7, 2017

Hi,

Here is the code to redirect to another page instead of displaying the "Record not found" message.

Change this code:

if (!$first_pageRecord) { dieWith404("Record not found!"); }

to this:

if (!$first_pageRecord) { header('Location: http://site.com/404.php'); exit; }

And add the change the URL to the page you want to redirect to.

That's it. Let me know if you have any questions or need anything else.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Damon - June 13, 2017

Hi,

I looks like redirecting back to the same page is causing issues.

Instead of displaying "record not found" or redirecting to the same page, you could display some content.

So, instead of having either this code:

if (!$first_pageRecord) { dieWith404("Record not found!"); } 

or this code:

if (!$first_pageRecord) { header('Location: http://kuecker.com/awards-test.php'); exit; }

You can display other content anywhere in the page with this code:

<?php if (!$first_pageRecord) : ?>

   Add content here..

<?php endif; ?>

Can you try this?

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/