[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create a login page in php

Posted on 2005-05-16
9
Medium Priority
?
234 Views
Last Modified: 2008-02-01
Hi,

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

Thanks!
0
Comment
Question by:TommyN14
  • 3
  • 3
  • 3
9 Comments
 
LVL 2

Expert Comment

by:dKasipovic
ID: 14018964
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...
0
 
LVL 2

Expert Comment

by:DBB
ID: 14022118
0
 
LVL 1

Author Comment

by:TommyN14
ID: 14033558
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!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:dKasipovic
ID: 14035858
<form action="<? echo $PHP_SELF; ?>" method="post">
0
 
LVL 2

Expert Comment

by:DBB
ID: 14038855
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
0
 
LVL 1

Author Comment

by:TommyN14
ID: 14042536
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
}
}
?>
0
 
LVL 2

Expert Comment

by:DBB
ID: 14044289
Hi,

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

Dbb
0
 
LVL 2

Accepted Solution

by:
dKasipovic earned 500 total points
ID: 14044639
first change if(!$submit) to if(!$_POST["submit"])

second change f($username == "user" && $password == "mypass") to f($_POST["username"] == "user" && $_POST["password"] == "mypass")

and finally change all ocurances of <?php echo $_SERVER['PHP_SELF']?> to <?php echo $_SERVER['PHP_SELF']; ?> (should work anyway, but just to be sure).

So your script should look like this:

<?php
//if they haven't pressed the submit button, then show the form
if(!$_POST["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($_POST["username"] == "user" && $_POST["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
}
}
?>

This should work... Hope I helped...
0
 
LVL 1

Author Comment

by:TommyN14
ID: 16077311
Sorry for taking so long to accept this answer.  I was on another project.  Got sidetrack.

Thank you!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question