how to go to the same page after login in PHP
You would need a way to keep track of the web pages visited, so you can back-reference to the last page the user has browsed to.
When I think of tracking a user's session across multiple-pages I, like every other PHP programmer, think of using sessions.
A possible way could be to store the link visited into a session variable and then when the user reaches the login.php
page (the page to login into) provide a header
redirect to $url
given by the session variable.
NOTE: Below code snippets, have not been tested or compiled.
You can paste this code into all your pages on your website:
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVER
variables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com/$url"); // perform correct redirect.
How to redirect to the same page after login
A simple solution would be to store the "return" URL in a session variable before you kick to the login page. The login page would check for the presence of the session variable and then unset it prior to using a header location re-direct to return the user to the URL in question.
For example on the login page you'd use:
// Successfully logged in...
$destURL = $_SESSION['kickurl'] ? $_SESSION['kickurl'] : '/index.php';
unset($_SESSION['kickurl']);
header('Location: ' . $destURL);
exit();
Redirect user to same page after login in a specific page
Try this..
In your authenticated method, check the previous request url is equal to route('congresses.registration'). If yes redirect to route('congresses.registration') with parameters.
LoginController:
use Illuminate\Support\Facades\Route;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated(Request $request, $user)
{
//check if the previous page route name is 'congresses.registration'
if(Route::getRoutes()->match(Request::create(\URL::previous()))->getName() == 'congresses.registration') {
//redirect to previous page with parameters
return redirect(Request::create(\URL::previous())->getRequestUri());
}
return redirect()->intended($this->redirectTo);
}
}
Hope it helps.. Let me know the results..
Reference here
Redirecting to previous page after login?
A common way to do this is to pass the user's current page to the Login form via a $_GET
variable.
For example: if you are reading an Article, and you want to leave a comment. The URL for comments is comment.php?articleid=17
. While comment.php
is loading, it notices that you are not logged in. It wants to send you to login.php
, like you showed earlier. However, we're going to change your script so that is also tells the login page to remember where you are:
header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
// Note: $_SERVER['REQUEST_URI'] is your current page
This should send the user to: login.php?location=comment.php%3Farticleid%3D17
. login.php
should now check to see if $_GET['location']
is populated. If it is populated, then send the user to this location (in this case, comment.php?articleid=17
). For example:
// login.php
echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
echo htmlspecialchars($_GET['location']);
}
echo '" />';
// Will show something like this:
// <input type="hidden" name="location" value="comment.php?articleid=17" />
// login-check.php
session_start();
// our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
$redirect = $_POST['location'];
}
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
$url = 'login.php?p=1';
// if we have a redirect URL, pass it back to login.php so we don't forget it
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location: " . $url);
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
$url = 'login.php?p=2';
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location:" . $url);
exit();
}
elseif(isset($_SESSION['id_login'])) {
// if login is successful and there is a redirect address, send the user directly there
if($redirect) {
header("Location:". $redirect);
} else {
header("Location:login.php?p=3");
}
exit();
}
Gotchas
You should run some validation against $_GET['location']
before sending the user there. For example, if I tell people who use your site to click on this link: login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php
... then they will be sent to a foreign URL that will try to do something bad.
Always make sure to use urlencode
when passing URLs as $_GET
parameters. This encodes special URL characters (such as ?
, &
, and %
) so that they don't break your url (e.g.: login.php?location=comment.php?id=17
<- this has two ?
's and will not work correctly)
How to redirect the user to the same page after login?
you need to use session for this, in your congresstest create add the following
$uri = $request->path();
$request->session()->put('last_uri', $uri);
then on successful login:
protected function authenticated(Request $request, $user)
{
$last_uri = $request->session()->get('last_uri');
// or just simply return redirect()->route('NEEDED URI');
return redirect()->route($last_uri);
}
php redirecting to same page after login failed
Your query only produces rows, that actually match username, password and type. So the while loop only runs if the credentials are correct. You need to put the redirect to index.php after it.
while($rand=$rezultat->fetch_assoc())
{
if($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'Admin') //verificare array
{
header("Location: index.php");header("Location: admin.php"); //muta in pagina admin.php
exit;
}else if ($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'User'){
header("Location: user.php");
exit;
}
}
header("Location: index.php");
Related Topics
Is There Open-Source Code for Making 'Link Preview' Text and Icons, Like in Facebook
How to Post Soap Request from PHP
Array Merge on Multidimensional Array
Best Way to Check If a Url Is Valid
Examples of Vulnerable PHP Code
Zip All Files in Directory and Download Generated .Zip
Read in Text File Line by Line PHP - Newline Not Being Detected
Best Practice for Working with Currency Values in PHP
What Http Status Code Is Supposed to Be Used to Tell the Client the Session Has Timed Out
How to Rename Column in Laravel Using Migration
How to Install All Required PHP Extensions for Laravel
Email PDF Attachment with PHP Using Fpdf
Try/Catch Block in PHP Not Catching Exception
Fatal Error: Cannot Use Object of Type Stdclass as Array In
Why Are Certain Types of Prepared Queries Using Pdo in PHP with MySQL Slow