Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How To Go About Redirecting

Featured Replies

Hey I'm just wondering the best way to go about redirecting as a have some process pages in my blog system and I want them to redirect to home or admin home or wherever I specify. I'd like to do with this with php.

Use the location header.

 

header('Location: destination.url');
exit;

splashtech : whilst that is correct, it is prone to failure ... for example if you set a cookie, or put the redirect after any non-PHP code...

 

if you use this:

$dest = 'mypage.php';
$redir = @header( 'Location: '.$dest );
if( !$redir ) {
require( "redirect.php" );
}

 

Redirect is just a blank page with the following tag in it's <head>:

<meta http-equiv="refresh" content="1;url=<?=$dest;?>" />

 

The easiest way to wrap it all up is as a function (eg redirect) which you can call with just the page name. You could go further and specify a different redirect page for different jumps, for example to display different texts.

I agree with you, but I gave my (admittedly short) answer assuming that nothing would be output before the redirect. Maybe this was a faulty assumption?

 

But certainly your way would get around the issue, although I'm not sure I agree with the include part. The reason being, if you have output something already, one would logically assume it was a fully validating (X)HTML page - and therefore, including another one (which has the meta tag) would create a weird mess where there are two heads and two bodys and so on.

 

The moral of the story is to not output anything if you intend to do a redirect.

 

However, to take php_penguin's suggestion of wrapping it up into its own function a stage further, one might do something like this:

 

function redirectWithMessage($dest, $msg)
{
$_SESSION['notice'] = $msg;
header('Location: " . $dest);
}

 

to be used like this:

 

// do stuff
$message = $stuff_was_successful ? 'Stuff saved sucessfully.' : 'Error with stuff';
redirectWithMessage('list.php', $message);

 

At the top of all pages, the contents of $_SESSION['notice'] can be checked for and retrieved if present, and then unset. This way, you can display success/failure messages from a given operation (a save to db for example) on the page.

 

Yeah - my code has MVC-framework-envy :p

 

Let me know if this didn't make sense - I'm not sure I explained myself particularly well. :)

 

Edit: also, this assumes you are initialising the session somewhere (in an included setup.php or similar, maybe?)

  • Author

thanks guys both great answers for both circumstances I appreciate the help

splashtech: you forgot to open your session ;)

 

Also, including the second file with a second head section doesn't confuse any of the current browserset, and redirect pages typically aren't required to be valid code.

 

Good stuff with that function though

splashtech: you forgot to open your session ;)

Hehe, thats what I said in my edit at the bottom of the post :D

 

Also, including the second file with a second head section doesn't confuse any of the current browserset, and redirect pages typically aren't required to be valid code.

 

Good stuff with that function though

Fair comment. I'm just picky with my validating :p Plus if you start serving content as xml rather than html (application/xml+xhtml or whatever it is), you end up in a whole different game in terms of validation and well-formedness. But you are correct when serving as text/html, and when it aint broke... ;)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.