简体   繁体   中英

How to make a user stay on same page once they log in

I am having an issue on how to make it where users who are viewing any page can log in on the page they are viewing and it stays on that page. How would this be accomplished?

Below is a single line I am currently using, however, if on any page and a user logs in, they are redirected to their profile. How can I set this line where it logs the user in, and it stays on that same page they are viewing? So in other words, are not redirected to their profile...

PHP:

header("Location: members.php?id=" . $_SESSION['username']);

If more info is needed, let me know and I can make an edit ;)

Have the login form submit the address of the current page. Then you can simply redirect back to that address when the login succeeds, eg

<form>
   <input type="hidden" name="curpage" value="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" />
   <input type="text" name="username" />
   <input type="password" name="password" />
   <input type="submit" />
</form>

if ($login_is_successful) {
   header("Location: {$_POST['curpage']}");
}

You could try using the referer, but since that's not sent by all browser, and is not always accurate, you're better off sing alternate "current location" tracking means, such as the above hidden form field.

When they click on the login button you can read the url and save it to a varibale and redirect them to this url.

So instead of

header("Location: members.php?id=" . $_SESSION['username']);

you can use sth. like:

 header("Location: $last_page);

Try this you can create a php file with this code and include into your code like this session.php

<?php
session_start();
error_reporting(0);
if(isset($_SESSION["usuario"]))
{
$usuario = $_SESSION["usuario"];
else{
header('Location: members.php?id=" . $_SESSION['username']');
}
?>

index.php

<?php
include ('session.php'); 
?>

to avoid using same code in every page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM