Page 1 of 1

simple login problem

Posted: Wed Mar 26, 2003 1:08 am
by seeker2921
Hello,

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.

Code: Select all

<?php 

  function validate_login ($user,$pass) &#123; 
    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&#1111;'password'] === md5($user.$pass) ... ) 
    $valid = array ( 
       'f1' => 'test', 
       'f2' => 'test' 
    ); 

    if ($valid&#1111;$user] === $pass) return true; 
    else return false; 
  &#125; 

  $user = $_SERVER&#1111;'PHP_AUTH_USER']; 
  if( validate_login($user, $_SERVER&#1111;'PHP_AUTH_PW']) )  &#123;  ?> 
     <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.. 

&#125;  // end auth-content 

else &#123;    // 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 
&#125;  // end no-auth-content 

?>

Posted: Wed Mar 26, 2003 9:00 am
by daven
try using header().

ex:
// if the folder is named after the username
$username=$_POST['username']
header("Location: http://www.yoursite.com/base/path/to/~" ... index.html");

Posted: Thu Mar 27, 2003 3:47 am
by seeker2921
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()

Code: Select all

<?
 if(($PHP_AUTH_USER == "user1") AND ($PHP_AUTH_PW == "test"))
 &#123;
header("Location: /~user1/"); 
 &#125; 
if(($PHP_AUTH_USER == "user2") AND ($PHP_AUTH_PW == "test"))
 &#123;
header("Location: /~user2/"); 
 &#125;
 else 
 &#123;
	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!"); 
 &#125;
?>

Posted: Thu Mar 27, 2003 8:54 am
by daven

Code: Select all

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

Code: Select all

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

Posted: Thu Mar 27, 2003 4:20 pm
by seeker2921
Thanx.. it didnt work at first and then i looked at the code and you had forgotten to add

header("WWW-Authenticate: Basic realm=\"My site\"");
header("HTTP/1.0 401 Unauthorized");

but when i added that it worked just fine.. thank you for your help!!!!