Creating a simple PHP login script with one page

programming with the MySQL environment and PHP or other server side languages.
Post Reply
darknkreepy3#
Site Admin
Posts: 253
Joined: Tue Oct 27, 2009 9:33 pm

Creating a simple PHP login script with one page

Post by darknkreepy3# »

There are some things to setup first when doing this type of login.

Image showing login script beginner bugs and solution
imgs/login_side-by-side.png
  • MySQL|PHP|APACHE installed & configured
    download and unzip phpMyAdmin
    enter the MySQL Server root and password [root][whatever you setup mysql with as pw]
    create your database [test]
    create your table in the new database [users]
    add [username] (no brackets)
    add [password] (no brackets)
    add an entry (row) like [johndoe][test1234]
Now that you have manually setup the database, table, fields, and test row, you can make a folder in your http docs main folder like [site1]

create an index.php page with a text editor like NOTEPAD++ (pc) or TEXTWRANGLER (mac)
save it to the [site1] folder

here is the index.php code

Code: Select all

<?php
	//set these to an empty string
	$username="";
	$password="";
	//check if the variables in $_POST were set
	//
	if(isset($_POST['username']))
		{$username=$_POST['username'];}
	//
	if(isset($_POST['password']))
		{$password = $_POST['password'];}
	//
	if($username!=""&&$password!="")
		{
		$connect=mysql_connect("localhost","root","bonjour")or die("Could not connect to database.".mysql_error());
		mysql_select_db("test")or die("Could not select database.".mysql_error());
		$query=mysql_query("SELECT * FROM users WHERE username='$username'");
		$numrows=mysql_num_rows($query);
		//
		if($numrows>0)
			{
			//there maybe multiple names entered but we just need a hit on the table for a good n:p
			for($a=0;$a<$numrows;$a++)
				{
				$row=mysql_fetch_assoc($query);
				$db_un=$row['username'];
				$db_pw=$row['password'];
				//
				if($username==$db_un&&$password==$db_pw)
					{
					echo "<a href=admin.php>ADMIN AREA</a>";//always end your anchors with <a href="link">text here</a>
					}
				}
		mysql_close($connect);//always close the mysql connection when you don't need it
		}
		//
		else
			{
			/*
			format your lines of output so it looks good to you when you VIEW SOURCE with newlines
			no need for a form, you can use a submit button with a simple anchor (a) remmber to end your anchor </a>
			single quotes for things needing quotations inside of double quote php output
			*/
			echo
				"
				<h3>Incorrect Name and Password</h3>
				<a href='index.php'><input type='submit' value='Try Again'></a>
				";
			}
		echo "ok";
		}
	//no name and password was sent, just show the login data this way instead of a different page
	else
		{
		echo
			"
			<h3>ADMIN LOGIN AREA</h3>
			<hr />
			
			<form name='login' action='index.php' method='POST'>
				<table>
					<tr><td>USERNAME</td><td><input name='username'></td></tr>
					<tr><td>PASSWORD</td><td><input name='password'></td></tr>
				</table>
				<input type='submit' value='LOGIN'>
			</form>";
		}
?>
goto addy on your localhost (127.0.0.1)
http://127.0.0.1/site1/
Post Reply