Cramster.com - Homework Solutions, Lecture Notes, Exams, and Free Online Homework Help
Sign Up Now! Login Customer Support Cramster Blog
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Problem Solved.
    Home    
    Homework Help    
   Answer Board   
    Resources (Beta)    
   
Member's Topic Headline:

Programing Question 2

Know the answer? Have a better solution? Share it.
Get Help Now.
View homework problems
explained for free!
Member Testimonials

Question:

Advertisement:

Answer | Ask New Question | Customize Profile | Leaderboards | 
FAQ

Member's Avatar

Scholar
Karma Points: 207
(virtual university)
Respect (76%):
Date Posted: 7/24/2008 7:43:11 AM  Status: Live
Programing Question 2
Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:

For login to VULMS (university web page for students login) you need your username (ID) and password. Write a simple program in PHP for user authentication on a web site. If you are not registered please mention the alternative path for registration (through coding). Please show the role of MYSQL data base in this example/program.

Answers:

Member's Avatar

Mentor
Karma Points: 611
Date Posted: 8/1/2008 3:01:45 AM  Status: Live
Asker's Rating: None Provided    Moderator's Rating: N/A
Response:
 
Dear..
 
  Login page:
    <? 
        // start session
            session_start(); 
       // convert username and password from _POST or _SESSION
            if($_POST)
              {
                  $_SESSION['username']=$_POST["username"];
                  $_SESSION['password']=$_POST["password"];  
               }
      
     // Sql query for a username&password 
         $result=mysql_query("select * from users where username='" . $_SESSION['username'] . "' and password='" . $_SESSION['password'] . "'"); 
     
     //     retrieve number of rows resulted
        $num=mysql_num_rows($result);  
     //    print login form and exit if failed.
         if($num < 1)
           {
               echo "You are not authenticated.  Please login.<br><br> 
                 <form method=POST action=index.php>
                          username: <input type=text name=\"username\"> 
                          password: <input type=password name=\"password\">
                     <input type=submit> 
                </form>"; 
             exit;
          }
    ?>

  Slq-Query

    CREATE TABLE USERS(  User_id int(8)unsigned NOT NULL auto_increment, username varchar(20) NOT NULL default '', password varchar(20) NOT NULL default '',
       PRIMARY KEY (user_id)  )TYPE=MyISAM

         note:  if you want to use any text editor, then  edit the connect.php with your mysql settings.

  connect.php

   <?
    // Login & Session example by sde connect.php
    // replace with your db info
          $hostname="localhost";
          $mysql_login="myusername";
          $mysql_password="mypass";
          $database="test";

        if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password)))
          {
               die("Can't connect to mysql.");    
             }
          else
             {
                if (!(mysql_select_db("$database",$db))) 
                   {
                      die("Can't connect to db.");
                     }
              }
       ?>

 I hope it is use full to you. If any doubts , just remember CRAMSTER is with you.


Member's Avatar

Scholar
Karma Points: 331
Date Posted: 8/1/2008 3:25:04 AM  Status: Live
Asker's Rating: None Provided    Moderator's Rating: Helpful
Response:

HTML code that will display the form

<html><head><title> User Login</title>
</head>
<form method="post" action="authenticate.php">
 <label for="username">Username: </label><br />
 <input type="text" name="username" id="username"><br />
 <label for="password">Password: </label><br />
 <input type="password" name="password" id="password">
<input type="submit" name="submit" id="submit">
</form>

</html>

 

 

 

 

authenticate.php that will validate the user.

Requirements : you will have to create the table name pass_table having username, password coloumn.

So the role of mysql will be storing of database for username & password

 

 

<?php
$uname = $_POST['username'];
$passwd = $_POST['password'];

$username="username";
$password="password";
$database="your_database";
$con = mysql_connect(localhost,$username,$password);

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT username,password FROM pass_table");
$val=0;
$pass_wrong=0;
while($row = mysql_fetch_array($result))
  {

 if(strcmp($row['username'], $uname) ==0 )
 {
 if(0 == strcmp($row['password'], $passwd)
 {
 echo "<br> Valid user";
 }
 else
 {
 $pass_wrong=1;
 }
 $val=1;
 }

  }

mysql_close($con);

if($val == 0)
{
 echo "<br> Not a valid user, Register here  link<a href=link>Click Here</a> ";
}
else
{
 if($pass_wrong == 1)
 echo "<br>Invalid password";
}

?>







By reading or posting messages on these forums, you are agreeing to the Answer Board's Terms of Service and Conduct (TSC).


About Cramster | Terms of Use | Privacy Policy | Contact Us | Press Room | Site Map | Support | Anti-Cheating Policy

Cramster.com is not affiliated with any publisher. Book covers, title and author names appear for reference only.
Copyright © 2008 Cramster, Inc.