Saturday, January 15, 2011

user login and registration codes in php

Firstly to start with we have to download and install the wamp 
and the lamp server as according to the os we are using,,
then open notepad and start coding (remember to make a dedicated 
folder to avoid ) messing up and loosing the codes,, strictly name the 
programs as instructed or they won't work,
you can yourself edit codes and make changes if you wish to...




This code connects to the database, save this as connect.php and we shall be using this whenever required.

<?php

//ob
ob_start();

//session
session_start();

//connect to database
$error "Problem connecting";
mysql_connect('server','username','password') or die($error);
mysql_select_db('phpacade_emailactivation') or die($error);

?>








Now what we are going to do is to make a registration page where the users of your website will be able to register themselves by filling the forms,,, you can make changes to the no. of fields and also the field specifications








<?php

include 'connect.php';

if ($_POST['register'])
{
 //get form data
 $username addslashes(strip_tags($_POST['username']));
 $password addslashes(strip_tags($_POST['password']));
 $email addslashes(strip_tags($_POST['email']));
 
 if (!$username||!$password||!$email)
    echo "Please fill out all fields";
 else
 {
    //encrypt password
    $password md5($password);
    
    //check if username already taken
    $check mysql_query("SELECT * FROM users WHERE username='$username'");
    if (mysql_num_rows($check)>=1)
       echo "Username already taken";
    else
    {
       //generate random code
       $code rand(11111111,99999999);
       
       //send activation email
       $to $email;
       $subject "Activate your account";
       $headers "From: alex@phpacademy.info";
       $body "Hello $username,\n\nYou registered and need to activate your account. Click the link below or paste it into the URL bar of your browser\n\nhttp://phpacademy.info/tutorials/emailactivation/activate.php?code=$code\n\nThanks!";

       if (!mail($to,$subject,$body,$headers))
           echo "We couldn't sign you up at this time. Please try again later.";
       else
       {
           //register into database
           $register mysql_query("INSERT INTO users VALUES ('','$username','$password','$email','$code','0')");
           echo "You have been registered successfully! Please check your email ($email) to activate your account";
       }

    }
 }
}
else
{

?>

<form action='register.php' method='POST'>
Choose username:<br />
<input type='text' name='username'><p />
Choose password:<br />
<input type='password' name='password'><p />
Email:<br />
<input type='text' name='email'><p />
<input type='submit' name='register' value='Register'>
</form>

<?php

}

?>












Now we have to start coding the login page,here copy and directly paste the code or make any modifications and save it as login.php




<?php

include 'connect.php';

$session_username = $_SESSION['username'];

if ($_POST['login'])
{
   //get form data
   $username = addslashes(strip_tags($_POST['username']));
   $password = addslashes(strip_tags($_POST['password']));
   
   if (!$username||!$password)
      echo "Enter a username and password";
   else
   {
    //log in
    $login = mysql_query("SELECT * FROM users WHERE username='$username'");
    if (mysql_num_rows($login)==0)
       echo "No such user";
    else
    {
      while ($login_row = mysql_fetch_assoc($login))
      {
  
       //get database password
       $password_db = $login_row['password'];
  
       //encrypt form password
       $password = md5($password);
       
       //check password
       if ($password!=$password_db)
          echo "Incorrect password";
       else
       {
          //check if active
          $active = $login_row['active'];
          $email = $login_row['email'];
          
          if ($active==0)
             echo "You haven't activated your account, please check your email ($email)";
          else
          {
           $_SESSION['username']=$username; //assign session
           header("Location: index.php"); //refresh
          }
       }
  
  
      }
    }
   }
}
else
{

  if (isset($session_username))
  {
   echo "You are logged in, $session_username. <a href='logout.php'>Log out</a>";
  }
  else
  {
   echo "
   <form action='index.php' method='POST'>
   Username:

   <input type='text' name='username'><p />
   Password:

   <input type='password' name='password'><p />
   <input type='submit' name='login' value='Log in'>
   </form>
   ";
  }

}

?>

Here this code will be used for the user activation and needs to be saved as activate.php,,,do not make any changes to the source cede here.


<?php

include 'connect.php';

$code $_GET['code'];

if (!$code)
    echo "No code supplied";
else
{
    $check mysql_query("SELECT * FROM users WHERE code='$code' AND active='1'");
    if (mysql_num_rows($check)==1)
        echo "You have already activated your account";
    else
    {
        $activate mysql_query("UPDATE users SET active='1' WHERE code='$code'");
        echo "Your account has been activated!";
    }
    
}
?>



If any of the registered user of your website wants to log out he/she can log out.
The code here has to be saved by the following name logout.php.
<?php

session_start();
session_destroy();

header("Location: index.php");

?>


keep visiting for the new codes
by.
Ankit Kumar
B.Tech
(Electronics and Communicatin Engineering).

22 comments:

  1. very nice. I needed it. plz upload code for mail server............and sending free sms using php.


    thanks.

    akash

    ReplyDelete
  2. Dind't understand a single bit of the above codes ;]

    ReplyDelete
  3. can i upload in blogger and how and wich can i upload please mail me
    mail id : vadthyasanjeev@gmail.com

    ReplyDelete
  4. 2 nd part of script
    error


    Notice: Undefined index: in C:\xampp\htdocs\sam\index.php on line 3

    ReplyDelete
    Replies
    1. Sarvan this error is basically a syntax problem somewhere in the program and might have crept in while making changes in the programs in nomenclature.
      http://hofars.com/public/index_project_page_disp.php?escort=42
      or
      http://hofars.com/public/page_product_disp.php?escort=104
      or
      http://hofars.com/public/index_project_page_disp.php?escort=59
      or
      http://hofars.com/public/page_product_disp.php?escort=49


      Above are some of the links for virus programming which u might also be interested in.

      Delete
    2. This comment has been removed by the author.

      Delete
  5. dude, where's index.php file codes??

    ReplyDelete
  6. great but i need to have a CAPTCHA image,

    ReplyDelete
  7. i need the CAPTCHA code in easy way...
    thanks in advace.

    ReplyDelete
  8. Log in and register form are speaking to database but new users are not being posted to the db. Can anybody help? Could i have changed any defaults in phpmyadmin?


    ReplyDelete
  9. Hey this is srinivas can u send me the code to my mail
    srinivasa.mca.85@gmail.com

    ReplyDelete
  10. not working for me.. im using xampp.. whats wrong?

    Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\try\connect.php on line 11

    Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\try\connect.php on line 11
    Problem connecting

    ReplyDelete
  11. Works just fine i tested and it's 100%
    Thanks ;)

    ReplyDelete
  12. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a9386356/public_html/register.php on line 21
    what to do?

    ReplyDelete
  13. where is index.php and which one is register.php.....

    ReplyDelete
  14. I'm a newbe, where's the sql file? No idea how to construct this table

    ReplyDelete
  15. wie heeft de sql voor database
    Users ofzo

    ReplyDelete
  16. Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\series\uks\core\functions\users.php on line 4
    i can't find my mistake help me

    ReplyDelete