0

I want my user to redirected to page where user clicked login so i added following code to config file so that each and ever file has access to redirect code

config.php

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

So that when ever i need a redirect i can simple call like bellow

header('location:' . $_SESSION['redirect_url']);
exit();

and when i try above code i get error showing

ERR_TOO_MANY_REDIRECTS

i even tried echo $_SESSION['redirect_url'] and it showed a valid requested URL

But it doesn't redirected. Can some one help me what wrong im doing.

and here is

.htaccess

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • And what is in your `.htaccess` file – RiggsFolly Jan 29 '20 at 17:42
  • @RiggsFolly updated with `.htaccess` in question. – sanoj lawrence Jan 29 '20 at 17:45
  • have you tried adding exit(); below the header() - https://stackoverflow.com/questions/3553698/php-should-i-call-exit-after-calling-location-header – PHPology Jan 29 '20 at 17:48
  • @PHPology yes `exit();` is present already still i get the redirect `error` – sanoj lawrence Jan 29 '20 at 17:52
  • You're most likely trying to redirect to the same file which would be the reason it's throwing that error. – Funk Forty Niner Jan 29 '20 at 17:59
  • @FunkFortyNiner yes i thought that, so i tried to echo `$_SESSION['redirect_url']` and i get URL of last visited page like `/product.php?id=1234` here is the working example http://safebrowser.tk/ – sanoj lawrence Jan 29 '20 at 18:02
  • Ok. Well, if this is an .htaccess related question, I won't be of much help. If you try it without relying on .htaccess and it works, then you'll need to further debug/research this as to why it's acting that way. – Funk Forty Niner Jan 29 '20 at 18:10
  • @FunkFortyNiner no this is not `.htaccess` related question i have added it because @RiggsFolly requested it. Can you please help me solving this. – sanoj lawrence Jan 29 '20 at 18:22
  • Ok. While Googling, I found [this answer](https://stackoverflow.com/a/37333326/1415724). That and other hits on Google. The search was `ERR_TOO_MANY_REDIRECTS php sessions htaccess`. – Funk Forty Niner Jan 29 '20 at 18:26
  • @FunkFortyNiner but i use `PDO` already – sanoj lawrence Jan 29 '20 at 18:36
  • What I posted has nothing to do with PDO, just this part of their answer: *Session sometimes depending on your system and your code, will not work unless you have a session_id("sessionID"), I used to have this problem too. it's a method and a way I used.* – Funk Forty Niner Jan 29 '20 at 18:39
  • @FunkFortyNiner i did `session_start(); session_id();` in my `config.php` still i get `redirect` error. – sanoj lawrence Jan 29 '20 at 18:44

1 Answers1

0

after long research.

echo "<a href='/login.php?ref=". urlencode($_SERVER['REQUEST_URI']) ."'>login</a>";

and in login.php

if ($_GET['ref'] != '') {
    $url = $_GET['ref'];
} else {
    $url = "/";
}

if ($user->login($username, $password)) {
        $_SESSION['username'] = $username;
        header("location:http://" .$url);
        exit();
    }

am using this simple method to redirect to requested page.

sanoj lawrence
  • 951
  • 5
  • 29
  • 69