php - How to restrict a web page to load on unsuccessful login?
Get the solution ↓↓↓Solution:
Once the login is validated by your php script you can redirect the user by header location method.
// Assuming $user->login() returns TRUE in case of success.
if ($user->login()) {
header('Location: http://yourdomain.tld/loginSuccess.php');
} else {
header('Location: http://yourdomain.tld/loginError.php');
}
If you want access control over loginSuccess.php you should use a cookie/session validation. The $user->login() method should write some data to keep the login active.
For example if you fill the user data in $_SESSION['user'] you can check if the user is logged in adding this code to the top of your pages:
session_start();
if (empty($_SESSION['user']) {
// In case of no data about the user in $_SESSION redirect to error 403 page.
header('Location: http://yourdomain.tld/403.php');
}
Answer
Solution:
You can add some restriction. Now depends that you are using any framework or using custom code. You can check if guest user is trying to access a page then redirect again to login page. Such as if you store user name in db and if user is logged in then check for example:
if($loggedUser->username !="" || $loggedUser->username !==NULL)
redirect to next page
else
redirect to login page.
For more detailed answer, show us your code please.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.