Jump to content

Recommended Posts

Hey guys,

 

The problem i am having is with navigating back to a previous page in php...

 

What i am trying to do is, when a user tries to go to a certain page, they have to be logged in, otherwise they can't access it. So what i do is check that the user is logged on, if they are not then they are directed to my login page where they can log in.

 

But in my script i have a header which directs them to the profile page, but i dont want them to go there, i want them to be directed back to the page that they were TYRING to access....

 

I really can't figure out how to do this.....searched google and stuff too, but nothing comes up in the example i am trying to do..

 

Can anyone give me insight on how to do this??

 

cheers

 

Craig

Link to comment
https://forums.phpfreaks.com/topic/114310-returning-to-previous-page/
Share on other sites

inside your condition that checks if user is logged in, before the header redirect, do

 

$_SESSION['currentpage'] = 'page.php';

 

or

 

$_SESSION['currentpage'] = $_SERVER['PHP_SELF'];

 

and then in your login script, where you send them to the default logged in page upon successful login, before the header redirect, do

 

$location = ($_SESSION['currentpage'])?  $_SESSION['currentpage'] : "profile.php";
header("Location: $location");

 

edit:

 

or yeah, you can pass it via GET method like darkwater mentioned

 

 

What? Remove the header redirect to the profile page... or simply have

 

if ( $loggedOn == TRUE )
   header( 'Location: profile.php' );
else
   header( 'Location: login.php' );

 

Store it in the URL when you redirect them to the login page.  I've seen sites with like:

 

login.php?redirect=somepage.php

 

A cleaner way would be to use sessions to track the previous page the user has been on, I do this all the time.

 

At the bottom of my scripts i have $_SESSION['lastPage'] = $_SERVER['REQUEST_URI'];

could use

 

<?php
// login.php
$page_just_on = $_SERVER['HTTP_REFERER'];
$query_string = $_SERVER['QUERY_STRING'] != '' ? '?' . $_SERVER['QUERY_STRING'] : '';
$redirect_page = $page_just_on . $query_string;
?>

 

store $redirect_page as a hidden input field in the login form.

then after processing the login and checking their details, just redirect to the page.

 

I use this method..

 

Even though it's been discussed in a way, just giving practical examples :)

 

 if(isset($_SESSION['url'])){
         $this->referrer = $_SESSION['url'];
}else{
         $this->referrer = "/";
}

 

Then call the referrer from outside the class... i.e

$session->referrer

 

Although I've only recently started using this method, so I don't know how effective it is, or reliable, etc..

 

If you guys figure out a better waty, let me know

hey guys,

 

cheers for your overwhelming response!! you were all a great help thankyou!!

 

I'm having a slight problem now to do with directories....

 

i have my login script within a folder called 'function' so when the login page is called, it goes:

 

/function/login.php

 

so when i try redirecting to a page not in this directory it can't find it, because it isn't the same directory as the login script, say if i was trying to redirect to index.php it would do the following:

 

/function/index.php

 

does anyone know how i go back through a directory??

 

cheers for your help guys

 

Craig

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.