Jump to content

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();
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.