1

I work in PHP. I want to redirect page after login to the last page that i want to visit, but I'm still stack at here in 5 hours and I still don't make it yet. This is the schema, I have 3 php file.

newest.php (before login), 
signin.php (before login), 
thread.php (after login). 

I'm using cookies for this redirecting. First i went to the newest.php, then i clicked the button (go to thread.php). Then thread.php saw that you haven't loggin yet, then redirected to signin.php. After i fill the signin form then, i clicked the submit button (the signin.php), then I'm stack at signin.php (not going anywhere) even after I've loggin in, it should be go to thread.php automatically.

this is my code in newest.php & thread.php (not in signin.php):

$coopage='coopage';
$current_page='http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
setcookie($coopage, $current_page,time()+86400,'/');

submit button in newest.php (it goes to thread.php):

echo "<center><button onclick=\"window.location='/thread/form'\">add new thread</button></center>"; 

in signin.php (after i clicked the submit button or in submit area, because form and after submit i made in the same page) (in the bottom of the page):

if(isset($_COOKIE[$coopage])){
    $url=$_COOKIE[$coopage];
    unset($_COOKIE[$coopage]);
    header('location:'.$url);
}

note: in signin.php i also have another cookie setup before this cookie, is that the cause of this? or does it matter if i have 2 cookies setup in one page? Another cookie setup is like this (at the top of the page)

$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year
Juna serbaserbi
  • 205
  • 2
  • 12

2 Answers2

4

I would not use cookies at all.

Method 1

A possible way could be to store the link visited into a session variable and then when the user reaches the login.php page, provide a header redirect to $url given by the session variable.

Paste this code into all your pages on your website or the main container.

<?php
session_start(); 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 

For the login page you can have:

<?php
session_start();  // needed for sessions.
if(isset($_SESSION['url'])) 
   $url = $_SESSION['url']; // holds url for last page visited.
else 
   $url = "student_account.php"; 

header("Location: http://example.com/$url"); 

Method 2

A simpler solution by far is simply to have:

<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />

Then redirect to that address once they log in.

However, this is only good if you have a login box on every page.

$_SERVER['REQUEST_URI'] will simply hold the current page. What you want to do is use $_SERVER['HTTP_REFERER']. So save the HTTP_REFERER in a hidden element on your form, but also take note on that in the PHP that processes the form you will need some logic that redirects back to the login page if login fails but also to check that the referer is actually your website, if it isn't, then redirect back to the homepage.

Method 3

Another common way to do this is to pass the user's current page to the Login form via a $_GET variable.

change your script so that is also tells the login page to remember where you are:

Note: $_SERVER['REQUEST_URI'] is your current page

header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));

Now check if it is populated, then send the user to this: login.php

echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
    echo htmlspecialchars($_GET['location']);
}
echo '" />';
//  Will show something like this:
//  <input type="hidden" name="location" value="previousPage.php" />

login-check.php

session_start();

//  our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
    $redirect = $_POST['location'];
}

if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
    $url = 'login.php?p=1';
    // if we have a redirect URL, pass it back to login.php so we don't forget it
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location: " . $url);
   exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
    $url = 'login.php?p=2';
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location:" . $url);
   exit();
}
elseif(isset($_SESSION['id_login'])) {
    // if login is successful and there is a redirect address, send the user directly there
    if($redirect)) {
        header("Location:". $redirect);
    } else {
        header("Location:login.php?p=3");
    }
    exit();
}
Darren Willows
  • 2,053
  • 14
  • 21
  • 1
    wow, your answer is a lot Darren, wait. I'm digesting your code. It's too hard for me, but I'm trying one by one. – Juna serbaserbi Nov 12 '15 at 13:03
  • Darren, method 1, my brain can receive it. And about method 2, I don't have login form in every page. Method 3, my brain can not be filled by this yet, maybe I learn it again later. So i will choose your method number 1. But Darren, I've heard that people told that **session** is server side, doesn't it matter to do this with session?. But by the way, how did you typing so fast for that answer, how amaze you are i think. – Juna serbaserbi Nov 12 '15 at 14:40
  • Darren, method 1, it works Darren, great, i like it. I give you +1 Darren, thanks for helping me. – Juna serbaserbi Nov 12 '15 at 14:57
1

Your new cookie => local storage, make sure to have this code on every page you what to log the url on except for the login page otherwise it will redirect you back to the login page.

var currentPage = window.location.href;
localStorage.lastPageVisited = currentPage

This is then the code for the submit button and it will redirect the user to the last page he visited.

$(document).ready(function() {
    $('button').click(function() {
        window.location.href = localStorage.lastPageVisited;
    });
});
coolguy
  • 7,866
  • 9
  • 45
  • 71
Zorken17
  • 1,896
  • 1
  • 10
  • 16
  • you could have mentioned that localStorage is a HTML5 feature..only HTML5 supported browsers will handle this – coolguy Nov 12 '15 at 13:04
  • One thing that you also want to make sure of is that you're using the HTML5 doctype. You shouldn't be able to use an XHTML doctype with HTML5 features. , Using this doctype should not impact your browser support. Also no need to add window.localStorage as browsers recognizes just localStorage as well. – Darren Willows Nov 12 '15 at 13:05
  • @Junaserbaserbi do you know what HTML5 is? – Zorken17 Nov 12 '15 at 13:07
  • @coolguy, the suport for HTML5 goes quite far back. For example explorer 8 has suport for it, and that is a quite old browser. – Zorken17 Nov 12 '15 at 13:10
  • isn't it Zorken? I don't really know Zorken, from the beginning i was not using – Juna serbaserbi Nov 12 '15 at 13:10
  • Zorken, I made it Zorken, in Darren way of method number 1 Zorken, i call you again if i have another trouble Zorken. I'll keep giving you +1 Zorken for your effort to helping me. – Juna serbaserbi Nov 12 '15 at 15:00
  • Zorken17 I need your help again, in [here](http://stackoverflow.com/questions/33688773/how-do-i-make-this-javascript-in-loop/33688938#33688938) Zorken, I need to convert php to javascript, but I can't. – Juna serbaserbi Nov 13 '15 at 09:35