We help IT Professionals succeed at work.

PHP Page Redirection (user logged in test)

edhasted
edhasted asked
on
Having written a load of web pages I want to put a test at the start of each page to check that the user is logged in. If not take them to the start page.

E.g.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php include("../includes/logged_in_test.php"); ?>

[rest of page follows on...]

with the code for "logged_in_test.php" looking like:

<?php
      session_start();
      if (!isset($login_session)) {
            header("Location: ../pages/start.php");
            exit();
            }
           else {
             return 0; }
?>  

The logic clearly works as with the exit(); line in place it blanks the screen but doesn't redirect me to start.php.

Am I doing something wrong or can this be done...



 
Comment
Watch Question

CERTIFIED EXPERT

Commented:
This can be done...for sure.

after redirection you wouldn't need to exit anyway which will not be executed. Also session_start(); should be once in the header page or config page.

Please refer the modified code below. Not if it is not redirecting to start.php page...please check your path.

Thanks
<?php
      session_start(); // this can be set at the home/config or master page
      $_SESSION['login_session'] = "assign something";

      if (!$_SESSION['login_session'])) {
            header("Location: ../pages/start.php");
      }
      else {
          // TO DO : dispaly logged in user content
      }
?>

Open in new window

Senior Developer
CERTIFIED EXPERT
Commented:
Adding through kalmax answers and most importantly,

session_start(); --> this line should be added before <!DOCTYPE line.

In the PHP manual, under session_start(), it says: To use cookie-based sessions, session_start() must be called before outputting anything to the browser.
Armand GSenior Developer
CERTIFIED EXPERT

Commented:
Rewriting your code:

<?php session_start(); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php include("../includes/logged_in_test.php"); ?>

<?php
      if (!isset($login_session)) {
            header("Location: ../pages/start.php");
            exit();
            }
           else {
             return 0; }
?>  

Author

Commented:
Hi, thanks for such fast responses. There is something funny happening in the code (mine) that I am investigating. Will report back soon.

Ed
Most Valuable Expert 2011
Top Expert 2016
Commented:

Commented:
check the directory.
since you're using ../pages/start.php I assume that this file is located somewhere, let's say so:

--WEBSITE
|____pages
|_________start.php
|____other
|_________yourcode.php

So if this code is in the same directory with folder "pages" you need to remove ".."
if the code you are executing this is in a directory named "whatever" and this directory is in the same directory as "pages" then you should find some other solution

Author

Commented:
Hi, many thanks to everyone.

1. This HAS to be on the top line, even below the DocType isn't high-up enough.
2. The pathing was fine. I use a unform structure so everything is always a ../ away...
3. I will be reading Ray's words and wisdom over the w/e end implements then soon, as always.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.