PHP Newsletter

Join now and receive weekly updates pertianing to the world of PHP.

Polls

What do you think about the new PHPMine Design?
Awesome
Not Bad
Sucks

PHP Gigs By City

PHP Jobs By Topic

Advertisements

How to write this PHP script?

Asked 2008-05-20 05:59:41 by sandydessert

There should be a form to enter a user's username and password with a login button. Let there be multiple users so there are multiple sets of username and password on entering any of which a particular php page should open up. These username and password sets will be stored in a table inside a MySQL database. So plz tell me how to write the php code which checks for valid username and password and then redirects to a page.

Anwered 2008-05-20 06:19:41 by Blue Rizla Girl

Use the $_POST[ ] array to get the values submitted in the form. For instance, if you had
<input type="text" name="user" />
then you would access this as $_POST["user"] (assuming you used the POST method on the form).

If $_POST["user"] is not set, then display a login form.

If $_POST["user"] is set then build up a query to read the stored, encrypted password from the database.

Encrypt the password the user entered with the crypt() function, using the encrypted password you retrieved from the database as the salt.

If the result matches the stored encrypted password then the password entered was correct, so display the content. Set a session variable (in the $_SESSION[ ] array) to indicate that the user is logged in correctly: $_SESSION[ ] cannot be modified by anything the user does on their end.

Otherwise the password did not match, so display a suitable message and a new login form.

Anwered 2008-05-23 00:24:18 by Rangacharyulu G

$conn=mysql_connect('host','user','passw...
mysql_select_db('database',$conn);

$sql1="select user_id from users where user_id='".$_POST['uid']."' and password=PASSWORD('".$_POST['pwd']."')";

$rs=mysql_query($sql1,$conn) or die("Can not execute: ".mysql_error($conn));

if(mysql_num_rows($rs)==0)
{
$msg="Invalid User ID / Password";
}
else
{
session_start();
$row=mysql_fetch_array($rs);
$_SESSION['uid']=$_POST['uid'];
}

Questions and answers provided by the Yahoo Answers Community.