faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

32 of 58 people (55%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

i want a register script than stores peoples username, password for them to login

Mar 1st, 2009 09:00
Sarah Turner, ha mo, Edwin Chan, chris anderson,


If you are using Apache, you may try to use Apache's htpasswd utility. 
Assume you are using Windows XP, Apache 2.0.49 and PHP 4.3.6, for 
example, you may create the following scripts:
Firstly, make sure you have a .htaccess file in the directory where 
you want the users to login, let's say C:\Website\HTML\protected1 is 
where you want to be password protected, add the following:
add .htaccess file with the following:
AuthUserFile C:\Website\Security\passwd.txt
AuthGroupFile C:\Website\Security\group.txt
AuthName "Logon Required"
AuthType Basic
<Limit GET PUT POST>
require group loginusers
</Limit>
Then, you need to create  C:\Website\Security\group.txt with the 
following line:
loginusers:
And then, you need to create a blank C:\Website\Security\passwd.txt.
Finally, copy this script to a website directory which you found 
suitable and modify the variables located at the top of the script:
<?php
   // ========== VARIABLES TO BE MODIFIED  =========== 
   $tempdir="C:\WINDOWS\TEMP";
   $passwdexe="C:\System\AS\bin\htpasswd.exe";
   $passwd_file="C:\Website\Security\passwd.txt";
   $group_file ="C:\Website\Security\group.txt";
   $group="loginusers";
   // ========= END OF MODIFIABLE VARIABLES ========== 
   // make sure the required information are inputted, or we show the 
input page instead
   if (!isset($_POST["user"]) || strlen($_POST["user"])==0 ||
       !isset($_POST["passwd"]) || strlen($_POST["passwd"])==0 ) {
         /* user and passwd not found, may be an error, or may be this 
is the first them
            you enter the page, you may add additional handlers here */
    } else {
     // make sure username is in correct format
     if (!preg_match("/^[a-zA-z1-9_]+$/",$_POST["user"])) {
         echo "Invalid User! Please click <a href='".$_SERVER
["PHP_SELF"]."'>here</a> to try again!";
         die();
     }
     // make sure the username is new   
     $failed=FALSE;
     if (file_exists($passwd_file)) {
       $handle = fopen($passwd_file, "r");
       while ($usrinfo = fscanf($handle, "%[a-zA-Z0-9_]:$apr1$%s\n")) {
         list ($user, $pass) = $usrinfo;
         if ($user==$_POST["user"])
                $failed=TRUE;
       }
       fclose($handle);
     }
     // reject if username already exists, or else insert it to the 
passwd file
     if ($failed==TRUE) {
         echo "User already exists! Please click <a href='".$_SERVER
["PHP_SELF"]."'>here</a> to try again!";
     } else {
         // add the user to passwd file
         $add_passwd="set temp=".$tempdir."&& set 
tmp=".$tempdir."&& ".$passwdexe." -b ".$passwd_file." ".$_POST
["user"]." ".$_POST["passwd"];
         $result = system("$add_passwd"); 
         // we now add user to specified group
         $handle = fopen($group_file, "r+");
         $counter=0;
         unset($tmparr);
         while ($grpinfo = fscanf($handle, "%[a-zA-Z0-9_ ]:%[a-zA-z1-
9,_ ]\n")) {
           list ($grp, $users) = $grpinfo;
           // add user to selected group
           $found=FALSE;
           if ($grp==$group)
             $found=TRUE;
           if ($found==TRUE) {
               $tmparr[$counter]=$grp.": ".trim($users)." ".trim($_POST
["user"])."\n";
           } else {
               $tmparr[$counter]=$grp.": ".trim($users)."\n";     
               if (trim($users)=="" && trim($grp)=="")  
                 $counter--;
           }
           $counter++;
         }
         // modifications done, we now write back to the group file
         rewind($handle);
         ftruncate($handle,0);
         for ($i=0;$i<$counter;$i++) {
            fwrite($handle,$tmparr[$i]);
         }
         fclose($handle);
         echo "Success! Please click <a href='".$_SERVER
["PHP_SELF"]."'>here</a> to insert another user!";
     }
     // we already processed the details, do not print out the html 
again
     die();
   }
?>
<html>
<body>
<center>
<form method=post name=adduser action=<?php echo $_SERVER
["PHP_SELF"]; ?>>
Username: <input type=text name=user value=''><br>
Password: <input type=password name=passwd value=''><br>
<input type=submit name=submit value=submit>
</form>
</center>
</body>
</html>
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com
http://voyagevacances.blogtact.com/
http://rejseferie.blogtact.com/
http://flyvningertilschweiz.blogtact.com/
http://volssuisse.blogtact.com/
http://volsromeitalie.blogtact.com/
http://viajesvacaciones.blogtact.com/
http://reisenchina.blogtact.com/
http://flugeschweiz.blogtact.com/
http://resasemester.blogtact.com/
http://viaggivacanze.blogtact.com/
http://reisenurlaub.blogtact.com/
http://reizenvakanties.blogtact.com/
http://volisvizzera.blogtact.com/
http://gartenpflege.blogtact.com/
http://geschenkideeen.blogtact.com/
http://vluchtenromeitalie.blogtact.com/