Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

How to obtain previous page's URL in php

I want to redirect to login page to previous page.I've tried many ways to make it work but I didn't. When I echo I can see on browser and it works properly. However, when I try to redirecting but I can't.



**login.page**


<?php



 if ($row = mysqli_fetch_array($result)) {
 $url = $_SERVER['HTTP_REFERER'];
 $url2 =parse_url( $url, PHP_URL_QUERY);
 header("Location: $_SERVER["HTTP_REFERER"]");



}

?>



**main.php**

<?php  
 
if (!isset($_SESSION['id'])) {
<a id='' href=' https://www.example.com/login.php>
<input type='submit'  name='send' i class='btn-basic  value='write'></a>
                          
}

?>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A lot of login pages I have seen use a query parameter to refer to what site/page redirected them to the login web page.

From the previous page you can go to the login page with a query url like "https://example.com/login.php?from=https://example.com/myhomepage" (obviously change the data to the referrer you are redirecting from). Then when you login you redirect back to that web page by accessing the GET parameter from like so:

header("Location:" . $_GET['from']);

(I'm a little rusty with my PHP so just let me know if the syntax is wrong).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...