Link to home
Start Free TrialLog in
Avatar of TommyN14
TommyN14

asked on

Create a login page in php

Hi,

I'm new to php and mysql.  How do I create a login page?  Please help with same codes.

Thanks!
Avatar of dKasipovic
dKasipovic

First you need a way of storing the usernames and password. I would recommend a MySQL database. Then you need to fill in some usernames and password (encoded string recommended, at least for password). Then in php you do the query to return the results. Example:

there is username: test and password:test123 in mysql database. php would be something like this

<?

mysql_connect("hostname", "username", "pass") or die("ERROR: Connection failed!");
mysql_select_db("users") or die("ERROR: Database not selected!");

$res=mysql_query("SELECT * FROM users WHERE user='".$_POST["username"]."' AND pass='".$_POST["password"]."'");
if (mysql_num_rows($res)>0) { ...redirect to the logged in page... }
else { echo "ERROR: Username and/or password incorrect. Please try again!"; }

?>

that would be the simple login script. You would need a form with text fields named username and password that will have action set to the file shown up there.

Hope I helped...
Avatar of TommyN14

ASKER

Hi,

Sorry for getting back so late.  Right now I'm having a problem that when I type in the username & pwd and click on Login, the page refresh itself...I checked the view source of the page and found out that the action attribute of the form tag is empty.  Here's the form tag: <form action="<?$PHP_SELF?>" method="post">.  What's wrong with it?

Thanks!
<form action="<? echo $PHP_SELF; ?>" method="post">
Hi,  
 
are you using the same page to handle the form or do you have a different page with the handling script on it?

If you're using the same page (script is on the same page as login): -

<form action="<?php echo $PHP_SELF; ?>" method="post">

or if you are using a seperate script file:-

<form action="/myscript.php" method="post">

Dbb
Hi,

I got this test code from one of the website, and try to test it out...When I click on Login, the page refresh itself and clear the data in the text boxes.  Any ideas?

Thanks!

<?php
//if they haven't pressed the submit button, then show the form
if(!$submit)
{
?>

<html>
<head>
<title>My Login Form</title>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']?>" method="post">
<div>
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" /><br />
</div>
</form>
</body>
</html>

<?php
}
else //otherwise, let's process this stuff
{
if($username == "user" && $password == "mypass") //if they got it right, let's go on
{
session_start();
session_register("mysessionvariable"); //set a variable for use later
$id = session_id(); //let's grab the session ID for those who don't have cookies
$url = "Location: page2.php?sid=" . $id;
header($url);
}
else //they got something wrong and we should tell them
{
?>

<html>
<head>
<title>My Login Form</title>
</head>
<body>
<span style="color:#ff0000;">Password/Username Is Invalid</span><br />
<form action="<? echo $_SERVER['PHP_SELF']?>" method="post">
<div>
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" /><br />
</div>
</form>
</body>
</html>

<?php
}
}
?>
Hi,

try <?php echo $_SERVER['PHP_SELF']?> instead of <? echo $_SERVER['PHP_SELF']?>

Dbb
ASKER CERTIFIED SOLUTION
Avatar of dKasipovic
dKasipovic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for taking so long to accept this answer.  I was on another project.  Got sidetrack.

Thank you!