PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a simple login problem.. What I want is when the user enters in there username and password they are taken to the index file of there own folder.so say there is user named "demo" and when "demo" logs in they are taken to the folder ~demo. Now im not sure how I should go about doing this.. all the code ive tried hasnt worked.. this is the last code I triend from someone on the fourm awile back.
<?php
function validate_login ($user,$pass) {
if (empty($pass) || empty($user)) return false;
// get logindata from somewhere or check with something else like a db (and use md5 etc instead, such as if ($database_table_rowї'password'] === md5($user.$pass) ... )
$valid = array (
'f1' => 'test',
'f2' => 'test'
);
if ($validї$user] === $pass) return true;
else return false;
}
$user = $_SERVERї'PHP_AUTH_USER'];
if( validate_login($user, $_SERVERї'PHP_AUTH_PW']) ) { ?>
<HTML>
<HEAD>
<TITLE>Welcome to Members Area</TITLE>
</HEAD>
<BODY>
<h3>You have successfully logged in!</h3>
<p>Here are your options: </p>
<?php
$group = array(
'f1' => 'folder1',
'f2' => 'folder2'
);
include ('/path/to/file/'.$group($user));
// or whatever it is you want this user to get or link or se etc..
} // end auth-content
else { // begin no-auth content
header('WWW-Authenticate: Basic realm="Subdomain"');
header("HTTP/1.0 401 Unauthorized");
?><p>This page is PROTECTED by HTTP Authentication.<br>
DO NOT TRY TO ACCESS THIS SITE IF YOU ARE NOT AUTHERIZED!</p>
<?php
} // end no-auth-content
?>
Im sorry but im still new to PHP.. I tried using header() and it does what I need it to when theres one user but when I add two or more it doesnt work.. heres the code i tried with header()
<?
if(($PHP_AUTH_USER == "user1") AND ($PHP_AUTH_PW == "test"))
{
header("Location: /~user1/");
}
if(($PHP_AUTH_USER == "user2") AND ($PHP_AUTH_PW == "test"))
{
header("Location: /~user2/");
}
else
{
header("WWW-Authenticate: Basic realm="My site"");
header("HTTP/1.0 401 Unauthorized");
print("This page is PROTECTED by HTTP Authentication.<br>\n");
print("DO NOT TRY TO ACCESS THIS SITE IF YOU ARE NOT AUTHERIZED!");
}
?>
<?php
# I suggest using && instead of AND. Different interpretations/precedence and all that
if(($PHP_AUTH_USER == "user1") && ($PHP_AUTH_PW == "test"))
{
header("Location: /~user1/");
}
# you forgot to use elseif for the second statement.
elseif(($PHP_AUTH_USER == "user2") && ($PHP_AUTH_PW == "test"))
{
header("Location: /~user2/");
}
else
{
print("This page is PROTECTED by HTTP Authentication.<br>");
print("DO NOT TRY TO ACCESS THIS SITE IF YOU ARE NOT AUTHORIZED!");
}
?>
<?php
# This will work if you only set $PHP_AUTH_USER for successful logins.
# Dynamic, so it will be easier to maintain
if(($PHP_AUTH_PW == "test") && (isset($PHP_AUTH_USER))){
header("Location: /~".$PHP_AUTH_USER."/");
}
else
{
print("This page is PROTECTED by HTTP Authentication.<br>");
print("DO NOT TRY TO ACCESS THIS SITE IF YOU ARE NOT AUTHORIZED!");
}
?>