Returning the user to the homepage after logging in?

I use PHP, I wanted to know if the user is on a specific page, and they need to log in or register in order to be able to do actions such as commenting or rating, etc. I wanted to return the user to the same page after logging in.

I thought to use the PHP function getand pass the url, I'm not sure if this is the best way to do this.

+3
source share
3 answers

Presumably your html form entry fields. When you create the form, put the current URI in a hidden input field, then you have the information available to you when you want to redirect after sending the POST data.

I usually don't use PHP without a framework, but here are some resources to help you get this information:

http://www.webcheatsheet.com/PHP/get_current_page_url.php

How to get the full URL of the current page on a Windows / IIS server?

Basically, it looks like this:

$current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];

But if you use https, you must change this line accordingly.

+3
source

An alternative is to redirect them back to $_SERVER['HTTP_REFERER']. Please note that the browser may not send the referral URL.

+2

, , ...

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . 'index.php';

I also forgot that you put this after, this is what will actually bring you back to the homepage

header('Location: ' . $home_url);
+1
source

Source: https://habr.com/ru/post/1771011/


All Articles