0

i have this code which i am using to send an approval to a user email. what i need is : when the user clicks on the link; he should first login and then he can proceed the approval process. I tried the following code to check if the session is not registered,then to force the user to login (index.php):

global $user_ID;
$user_ID = $_SESSION['empName'];
if ($user_ID == '') { 
header('Location: index.php'); exit(); 
} 
echo "<INPUT type='hidden' name='Code' size = '6' value ='hr1.php' />";
if(isset($_POST['Code'])){$Code = $_POST['Code'];}
mysql_close($con);

now i need a method to redirect user to the previous page (link)

any ideas???

Ahmed Ali
  • 977
  • 1
  • 12
  • 25

4 Answers4

1

While redirecting to login page then store a session value called return_url

$_SESSION['return_url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Then if login is successful then check if return_url exists / not. If exists then return back to $_SESSION['return_url']

header("Location: ".$_SESSION['return_url']);
HADI
  • 2,829
  • 1
  • 26
  • 26
  • Worked Perfectly :) i did : $_SESSION['return_url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $user_ID = $_SESSION['empName']; if ($user_ID == '') { header('Location: index.php'); exit(); } on the link page,and : if(isset($_SESSION['return_url'])) { header("Location: ".$_SESSION['return_url']); } else { header("location:Main_Login.php"); } – Ahmed Ali May 27 '13 at 11:50
0

Why don't send your current url as parameter when he clicks the link and it's not logged in?

I mean, that you must add a JS, for example, that when you click the link, add to your url a parameter like: ?lastUrl=/foo/foo.com

Then, you save the url into a variable, check if the user is logged in or not and return the variable (last url).

JS suggestion:

Without jQuery

window.onload = function() {
    var element = document.getElementById('checkLogin');
    element.setAttribute('src') = element.getAttribute('src') + '?' + 
        window.location.href;
}   // or you can add it into onclick method of your link,
    // but I think this is easier.
DaGLiMiOuX
  • 889
  • 9
  • 28
  • and what if it's not a fixed url? – Ahmed Ali May 27 '13 at 12:03
  • @AhmedAli What is the question? If you use this into your web page where you have the link, it will take `href` value. That value will be your current url, the url where you are right now. It doesn't matter if it's `/foo.com` or `/foo2.com` etc. – DaGLiMiOuX May 27 '13 at 13:58
0

Just make a make for the authorization and include this file on every page were needed.

auth.php

<?php
session_start(); //remove this if you do this somewhere else
if (empty($_SESSION['empName']) && !isset($_SESSION['empName'])) {  
header('Location: index.php'); exit(); 
}

page.php

<?php
include 'auth.php';
echo "<INPUT type='hidden' name='Code' size = '6' value ='hr1.php' />";
if(isset($_POST['Code'])){$Code = $_POST['Code'];}
mysql_close($con);
Perry
  • 11,172
  • 2
  • 27
  • 37
  • You will need `session_start()` to use sessions. After this it should work, and if not can you explain what doesn't work? – Perry May 27 '13 at 11:58
  • i did,what didn't work out was the redirection back to the first page that user gets on the mail (link page) – Ahmed Ali May 27 '13 at 12:02
0

You can save the current page to a session before redirecting the user to the login page: $_SESSION['current_url'] = get_current_url(). See get_current_url() here.

Once login, you can just redirect the user to the previous page:

if(isset($_SESSION['current_url'])) {
  header("Location: ".$_SESSION['current_url']);
} else {
  header("Location: index.php");
}
Community
  • 1
  • 1
Neigyl R. Noval
  • 6,018
  • 4
  • 27
  • 45