3 Replies - 1421 Views - Last Post: 13 March 2013 - 05:49 AM Rate Topic: -----

#1 josefroyadvincula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-December 12

Page not redirecting to index.php

Posted 11 March 2013 - 06:54 PM

Hi guys, I'm trying to create a login sort of app. So the user logs in at index.php and then the details are sent to login_handler.php. If login_handler.php doesn't find anything wrong, the page will be redirected to records.php

Now the problem is, I can directly access records.php without even going through the login page (via the address bar).

In my login_handler.php, I have this line of code
    if ( $result == 1 ) {
        session_start();
        $_SESSION["user"] = $username;
        header("location: ../records.php");
    } else {
        header("location:../index.php?login_failed==true");
    }



and in the records.php, this
session_start();
if ( isset($_SESSION["user"]) == false ) {
    header("location: index.php");
}



So why can I still access the records.php? :dozingoff:

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Page not redirecting to index.php

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,658
  • Joined: 23-August 08

Re: Page not redirecting to index.php

Posted 12 March 2013 - 03:46 AM

Whenever you call header, you should add exit(); immediately after to stop the rest of the page from executing.

This information is right in the manual:

Quote

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Was This Post Helpful? 1
  • +
  • -

#3 ryant  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 5
  • Joined: 08-March 13

Re: Page not redirecting to index.php

Posted 12 March 2013 - 04:36 AM

Hi mate,

please try this in your records.php.

session_start();
if ( !isset($_SESSION["user"])) {
    header("location: index.php");exit;
}



Hope that helps!:)
Was This Post Helpful? -1
  • +
  • -

#4 josefroyadvincula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-December 12

Re: Page not redirecting to index.php

Posted 13 March 2013 - 05:49 AM

Well, I would like to thank both of you for pointing out that exit(); line for me. I admit I wasn't aware of that, lol, so thank you both!

However, I realized the issue had to do with the browser cache. I can only access records.php if I had successfully logged in at an earlier time. Otherwise, it does as expected.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1