How can you redirect another page upon login using PHP?

Let me explain my situation: I don’t know if it makes a difference. I did not connect with a database. Instead I use a document call install.php to create table and all the data will be saved as a .json file under my data folder inside the MAMP. An example of the codes in install.php is as follow:


echo "setup admin account...";

 $users = [
[ 'username' => 'hannah', 'password' => '123'],
 [ 'username' => 'beeno', 'password' => '123']
$text = json_encode($users);
 file_put_contents('../data/users.json', $text);

I have two different home screens, basically one for index.php and another one for adminhome.php which the admin login will automatically direct to this page.

My login.php code is as follows:


 $users=json_decode($text, true);

 foreach($users as &$user){
     if($_POST['username'] == $user['username'] && $_POST['password'] == $user['password']){
        echo 'matched';
        //setcookie('username', $user['username']);
         $_SESSION['username'] = $user['username'];
         *echo "<p><a href='/'>Back to home</a></p>";*
echo 'not matched';
 echo "<p><a href='/'>Back to home</a></p>";

So for this specific block:
$_SESSION[‘username’] = $user[‘username’];
*echo “<a href='slash>Back to home


I want to direct to “adminhome.php.” I tried echo

<a href= (double quotations) adminhome.php (double quotations) Back to Home

. But immediately my website pop the 403 response code. What can I do? Thanks in advance!

header(“Location: some-other-page.php”);

Also, you should not use DIE command except for testing. And, your code is oddly formed.
Normally, you keep your user info in a table named something like “users” or whatever.
Then, to validate a login, you just run a query using WHERE username and password. If there are no rows returned, then the login is not valid. If it returns a row of data, then you are all set. You do not parse thru all the user’s to find the correct one. Lastly, if you query the user the 403 code would be in an ELSE process. You got the 403 response because your code will ALWAYS show it. It is not in an ELSE phrase but in the main code.

Hope all this helps!

Hello Alex,

Thanks so much for your assistance and advice. This is for my system design and implementation class. I have never touched html5, css and PHP up till few days ago. Certainly many flaws, and i have now connect it to a database and make it more structured as you have mentioned. Thanks a lot!!

Well, you are welcome! As a beginner, you might want to go here and study different sections of your code. This site is not the best place for professional code, but, it does have a large amount of information in an easy to locate manner. You can select any language and any area or search for examples. I think at your early level, this site might help you get started. But, please post here if you need help. That is what we are here for. Login-Form-Tutorial

Sponsor our Newsletter | Privacy Policy | Terms of Service