logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



PHP login

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
acid.-.burn



Joined: 15 Jan 2007
Posts: 17

PostPosted: Fri 06 Mar '09 5:19    Post subject: PHP login Reply with quote

can some one tell me what i am doing wrong with this code

config.php

Code:
<?php
$username = array('Admin', 'User');
$password = array('adminpass', 'userpass');
?>


index.php

Code:
<?php
require ("config.php");
session_start();
if ((!$username) || (!$password))
{
   echo '<form name=login method=post action="">
   user:<input type=text name=user><br>
   pass:<input type=text name=pass><br>
   <input type=submit value=go>
   </form>';

}
else
{
   if($user==$username && $pass==$password)
   {
      session_register("username");
      session_register("password");

      echo "Congratulations &username, you have logged in!<br>
      <a href=\"?logout=1\" >Logout</a>";

   }
   else
      echo "Incorrect Password please <a href=?index.php?>try again</a>";

}
if($logout==1)
{
   session_unregister("username");
   session_unregister("password");
}
?>
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Fri 06 Mar '09 10:37    Post subject: Reply with quote

The wrong part
Code:

if($user==$username && $pass==$password)


That should be

Code:

if($_POST['user']==$username && $_POST['pass']==$password)


Not tested, butitshould work. If not please post again.
Back to top
blubber



Joined: 29 Mar 2009
Posts: 2

PostPosted: Sun 29 Mar '09 7:25    Post subject: Reply with quote

Quote:
if($user==$username && $pass==$password)

Quote:
if($_POST['user']==$username && $_POST['pass']==$password)


Hm, I'm not much of a PHP guy, but I would be pretty amazed if either one of these solutions will work, because $password and $username are arrays, aren't they? I would suggest creating a slightly more complex and maybe more intuitiv data structure holding up user/pass and traverse them via a foreach($user_list AS $user) and than do the check acid was talking about OR just loop both arrays via for($i=0;...) eg.:

Code:
$success = 0;
for($i=0;$i<count($username);$i++){
   if($_POST['user']==$username[$i] && $_POST['pass']==$password[$i] )
   {
      session_register("username");
      session_register("password");

      $success = 1;
      break; // no reason to continue, we are already done
   }
}

if($success){
//give him root
}else{
//give him hell
}
Back to top
LuMorehead



Joined: 29 May 2009
Posts: 2
Location: USA

PostPosted: Fri 29 May '09 20:48    Post subject: Reply with quote

Also it never hurts to access the php documentation at php.net - Arrays and the use of POST() are well explained.
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1