0

i have a function that echo $_SESSION value in login page and store $_SERVER['REQUEST_URI'] as $_SESSION value on other page and it works fine.

But i need to call redirect_after_login function in all product page so i just added line after redirect_after_login

So my function.php file will look link this

<?php

session_start();
$key = '';

function redirect_after_login($value) {
if (!empty($value)) {
    echo $_SESSION['url'];
  } else {
      echo $_SESSION['url'] = $_SERVER['REQUEST_URI'];
  }
}

$activate_finction = $key == true ? 'Yes' : '';
$url = redirect_after_login($activate_finction);

And in login.php i added

$key .= 'login';//calling redirect function with value so it should send session value


if (isset($_POST['submit'])) {
        // login function skipped it....
            header('location:' . $url);
            exit;
        } else {
            $error[] = 'Wrong username or password or your account has not been activated.';
        }
    }

Since $key .= 'login'; has value

$activate_finction = $key == true ? 'Yes' : '';
$url = redirect_after_login($activate_finction);

Function is now called with ==true so i should receive stored value but still i get requested_url

My problem is in all page i get $_SESSION['url'] = $_SERVER['REQUEST_URI']; this value.

How do i get session value login.php and in all product pages function should store requested_url as session value.

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • Please add some code of your product pages. – Badrinath Jan 22 '20 at 11:59
  • that is what am trying to do i just call `` in `product.php` by default `$info = $user == true ? 'Yes' : ''; $url = redirect_after_login($info);` function i called here, since `$user = '';` has no value `ifelse` should store and get value – sanoj lawrence Jan 22 '20 at 12:03
  • Since actually `$user .= 'login'` has a value, but not `$key`, then `$activate_finction` will be always empty. – mitkosoft Jan 22 '20 at 12:23
  • @mitkosoft sorry forgot to update. by default the `function` is called without `$key` so `function` stores `requested_URL` and in `login.php` am adding a `$key .='login'`, since in login page we added a value in `key` `function` should give us `$SESSION` data instead i get `$_SESSION['url'] = $_SERVER['REQUEST_URI'];` this is the problem. – sanoj lawrence Jan 22 '20 at 12:29
  • Let me guess, you are `include` function.php in your login.php? So your code `$activate_finction = $key == true ? 'Yes' : ''; $url = redirect_after_login($activate_finction);` should be placed into your `login.php` in order to call this function AFTER your `$key .= 'login';` definition. – mitkosoft Jan 22 '20 at 12:41
  • @mitkosoft yes that's the problem how do i solve, only on login page i should get `session_value` – sanoj lawrence Jan 22 '20 at 12:45
  • @sanojlawrence, see below. – mitkosoft Jan 22 '20 at 12:59

1 Answers1

1

I refactor the code, so here you are a complete example:

function.php:

<?php
    session_start();

    if (isset($_SERVER['HTTP_REFERER']) && basename($_SERVER['HTTP_REFERER']) != basename($_SERVER['PHP_SELF'])) {
        $_SESSION['url'] = $_SERVER['HTTP_REFERER'];
    }

    function redirect_after_login() {
        header('Location:' . $_SESSION['url']);
    }
?>

product.php:

<?php
    include "function.php";
?>
<html>
    <head></head>
    <body>
        This is <?php echo $_SERVER['PHP_SELF']; ?> file.<br><br>
        <input type="button" name="login" value="Login" onclick="location.href='login.php'">
    </body>
</html>

login.php

<?php
    include "function.php";

    if(isset($_POST['submit'])){
        if($_POST['username'] == 1 && $_POST['password'] == 1){
            //success
            $_SESSION['logged'] = 1;
            redirect_after_login();
        }else{
            //error
            $_SESSION['logged'] = 0;
            $error = 'Wrong username or password';
        }
    }else{
        unset($error);
    }
?>
<html>
    <head></head>
    <body>
        This is <?php echo $_SERVER['PHP_SELF']; ?> file.<br><br>
        <form method="post" action="login.php">
            User: <input type="text" name="username"><br>
            Pass: <input type="password" name="password"><br>
            <input type="submit" name="submit" value="Login">
        </form>
        <?php
            if(isset($error)) echo "ERROR:" .$error;
        ?>
    </body>
</html>

Some short explanations:

  • to test the redirect logic, you can load product.php?id=258 initially;
  • $_SESSION['url'] is keeping HTTP_REFERER - the page you are coming from;
  • login.php is calling redirect_after_login() function on successful login (currently with user:1/pass:1, just for the example)
  • on successful login, there is an addition SESSION variable logged which you can use further (like to hide LOGIN button onto product.php page)
  • on bad login, there is an error displayed, but you the user stays on login screen.
  • $_SESSION['url'] is overwritten every time when you are visiting a different page and only then. This prevents losing HTTP_REFERER in case of bad login (when you have an actual refresh and HTTP_REFERER becomes the page itself)

Hope this helps.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • what about product.php ? how will i store `$_SERVER['REQUEST_URI']` in session – sanoj lawrence Jan 22 '20 at 13:01
  • just call `redirect_after_login()` with empty value either in your `product.php` or right after function declaration in `function.php` (if you don't wanna call it in every page). – mitkosoft Jan 22 '20 at 13:03
  • Yes i don't want to call in every product page, How do i do that can update the answer – sanoj lawrence Jan 22 '20 at 13:06
  • Not sure what do you mean with `How do i do that can update the answer`? – mitkosoft Jan 22 '20 at 13:08
  • this one `just call redirect_after_login() with empty value either in your product.php or right after function declaration in function.php (if you don't wanna call it in every page).` without call on ever page . – sanoj lawrence Jan 22 '20 at 13:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206448/discussion-between-sanoj-lawrence-and-mitkosoft). – sanoj lawrence Jan 22 '20 at 13:29
  • i tested your code in `localhost` it worked but when i tried in `web-server` i get error showing `too_many_redirect` https://stackoverflow.com/questions/59972656/unable-to-redirect-after-login-in-using-serverhttp-referer-in-php – sanoj lawrence Jan 29 '20 at 18:00