Jump to content

Login System with redirect to specific user page.


fantasticham

Recommended Posts

I know this is basic stuff but there's a specific way I want to do things that's making this awkward. To begin with, I want to create a login page, just using php with a small list of three users and no way to register. I can do that easily enough, but I want the page to redirect to a specific page for that member once they've logged in. So if I were to login with FantasticHam, it would redirect to website.com/fantasticham/upload.php. Keep in mind I'm trying to keep this as simple as possible and I don't have access to a MySQL database.

 

I am pretty new to PHP, so excuse my ignorance.

<?php
session_start();
$users['FantasticHam'] = array('password' => 'iop123', 'redirect' => '/fantasticham/upload.php');
$users['JohnDoe'] = array('password' => 'abc123', 'redirect' => '/john/index.php');

if(array_key_exists($_POST['username'],$users)) {
if($_POST['password'] == $users[$_POST['username']]['password']) {
	$_SESSION['loggedIn'] = true;
	// redirect
	header('Location:'.$users[$_POST['username']]['redirect']);
	exit();
}
else {
	// invalid password
}
}
else {
// invalid username
}
?>

<?php
// pages that require login
if(!$_SESSION['loggedIn']) {
header('Location:/login.php');
exit();
}
?>

Brilliant, thanks for your help. What file to I add this too? The original log in page?

 

Is this alright for the login form?

<form method="post" name="frmLogin" id="frmLogin">
<table width="344" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150">USERNAME:</td>
<td width="194"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td width="150">PASSWORD</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td width="150"> </td>
<td><input type="submit" name="btnLogin" value="Login"></td>
</tr>
</table>
</form>

 

Sorry for asking really stupid questions, I just need a little more instruction.

Alright, I got it working with the form and the first code, it redirects fine. However, when I added the pages that require login code to the upload.php file, the login doesn't work, it just stays on login.php.

 

Adding the second code to the page I want to require login has stopped it redirecting for some reason.

 

Any ideas?

 

http://alexisarts.net/login.php

I've added two users.

USERNAME: withcode PASSWORD: password, doesn't redirect.

USERNAME: withoutcode PASSWORD: password, does.

 

<?php
// pages that require login
if(!$_SESSION['loggedIn']) {
   header('Location:/login.php');
   exit();
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.