PHP coding help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post by social_experiment »

Here's the basic process for a login system

1. User accesses a form (that's main_login.php for you)
2. Form is submitted to process page (checklogin.php for you)
3. User is logged in, redirect to logged in page at which login form is no longer needed

Does the current script work i.e let's a user log in?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: PHP coding help!!

Post by dsnraju »

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php

if($_POST['Submit'] == "Login")
{
$sel_user = "select * from usertable where username = ".$_POST['myusername']." and password = ".$_POST['mypassword'];

$res_user = mysql_query($sel_user);
$count = mysql_num_rows($res_user);

if($count > 0)
{
echo "<script>";
echo "location.replace('main.php');";
echo "</script>";
}
else
{
echo "<script>";
echo "alert('Wrong Username or Password');";
echo "location.replace('checklogin.php');";
echo "</script>";
}


}

?>


Try this code and solve your problem.

Acoording to this code you need PHP Sessions also.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP coding help!!

Post by social_experiment »

Yes, very rudimentary but dsnraju has the same idea as i do, your code is probably ok but your login logic is not quite there yet.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply