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.

Redirect Page using PHP within HTML

Featured Replies

Hello,

 

I am looking to run a custom PHP function amongst HTML which checks to see if the SESSION username has been set. If it has not been set I want to redirect to the index page.

 

This is my function code which I am calling;

 

function redirect() {
if(isset($_SESSION['username'])) {
} else {
header("location: index.php");
die();
}
}
The only problem I am coming across is that the redirect does not work. I did some research and found that if the 'header("xyz")' is called after the HTML tag that it will not work. I could not find any other way of running redirects through PHP.
Any help would be fantastic.
Ultimately I am trying to make it so that if the url (Of the accounts page, or any thereafter) is entered without a user being logged on; I want to redirect back to the index page. If there is a better way of accomplishing this then do tell. My knowledge of PHP is limited and being self taught the only way I can learn is by being told the best practise. So thank you for reading and I hope for some answers soon.
- MjA -

Try to run all of your PHP checks before you output html, but if this is not possible for you you can do something like

 

echo '<script>document.location="login.php";</script>';
exit;

this will redirect the user, and if they have JS disabled it will not load the rest of the page

This should work.

function redirect() {
    if(!isset($_SESSION['username'])) {
       header("Location: http://www.yourdomain.com/index.php");
       exit();
    }
}

The header information such as 301 is not required for internal stuff like this. This function call must happen before the opening <html> output.

  • Author

@D4Y0 - I incorporated this method but it just wasn't 'clean' enough for my liking. It would begin to load the page and then the function would run. The fail-safe was a good idea but it would give the impression of 'broken' site.

 

But thank you. I completely forgot about javascript since I have been flooding myself with PHP.

 

 

@BrowserBugs - Because of the above I realised that I had no choice but to re-think how I was doing things. Your code came in useful. I had to do a little research into what it meant using an exclamation mark before the function. I was curious how I could say 'IS NOT' in PHP.

 

I thought about how I was making my function work and ultimately shifted some things about and incorporated your code. This is what I ended up doing:

 

$section = "index";
session_start();
include('inc/functions.php');
redirect();
And it works like a charm.
Many thanks to you both for teaching me new things and helping me with this issue.
- MjA -

Your welcome. Quite often there is more than one way to do something, it's choosing the most reliable for the application that counts. I normally sketch out my functions and loops on a piece of paper helps as otherwise I find I loose track of what is what.

Not to be a pain but to warrant even creating a function for redirecting id change the function to look something like this...

 

function redirect($loc) {
    //Only redirect using PHP if headers arn't sent otherwise use javascript
    if(!headers_sent()){
    header("Location: $loc");
    exit;
    }else{
    echo "<script>window.location='$loc'</script>";
    }
}

the idea of functions are to be reusable that way if you wanted to redirect to different locations throughout your site you could just plug in the location instead of having to actually change the code inside the function or instead of having to have multiple redirect functions.

 

now you can use the above like this...

 

$section = "index";
session_start();
include('inc/functions.php');
//Redirect
if(!isset($_SESSION['username'])) {
redirect("http://www.yourdomain.com/index.php");
}

Edited by webdesigner93

Can I just tag a quick question on a related note? I've written a url checker and have the following (cutdown);

 

if($totalcount >= 1) {
   header("HTTP/1.1 301 Moved Permanently"); 
   header("Location: http://www.webaddress.com/movedpage.php");
   exit();
} else {
   // Gives 302?
   header("HTTP/1.0 404 Not Found");
   header("Location: http://www.webaddress.com/404.php");
   exit();
};

The 404 is not right, gives a 302 but the following gives a blank page?

 

if($totalcount >= 1) {
   header("HTTP/1.1 301 Moved Permanently"); 
   header("Location: http://www.webaddress.com/movedpage.php");
   exit();
} else {
   // Gives blank page
   header("HTTP/1.0 404 Not Found");
   exit();
};

The 404.php is set up correctly through htaccess and regular stuff like /nopage.php gives the 404 header and 404.php page. What's the obvious I'm missing here?

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.