Login and Password with PHP

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

Post Reply
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Login and Password with PHP

Post by Mexican Hat »

I pretty much know nothing about PHP so I need some help making a code that allows users to login to their account. If anyone could help me that would be great. I'm not really going to have a lot of members on my site so I just need a simple code.

Peace 8)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

put this into a script login.php and in all your other pages include it in the first line as

<? include ('login.php'); ?>

Code: Select all

<?php
	function valid_login($username, $password) {
		return ($username == 'elipollak' &&  $password == 'pass');
	}

	function login_screen($failed = 0) {
		$output  = "<HTML>"
			."<HEAD><LINK REL=StyleSheet HREF="_style.css" TYPE="text/css"></HEAD>"
			."<BODY bgcolor=#e1e1e1><center>"
			."<form action='". $_SERVER['PHP_SELF']. "' method='post'>"
			."<table border = 0>"
			."<TR><TD>Username:<TD><input type='text' name='username' maxlength='20'>"
			."<TR><TD>Password: <TD><input type='password' name='password' maxlength='20'>"
			."<TR><TD colspan=2><center><input type='submit' name='submit' value='Login'>"
			."</table>"
			."</form>"
			."<BR><BR>";

		if ($failed) $output .= "<font color = red><b>Failed ".$_SESSION['count']." time(s) : Keep Trying Biatch</b></font>";

		return $output ."<BR><BR></center></body></html>";
	}


	session_start();
	if( ! isset($_SESSION['count']) ) $_SESSION['count'] = 0;
	else $_SESSION['count']++;

	if( ! isset($_SESSION['logged_in']) ) {
		if ( ! isset($_POST['submit'])) {
			die (login_screen());
		} elseif ( valid_login($_POST['username'], $_POST['password']) ) {
			$_SESSION['count']  = 1;
			$_SESSION['logged_in']  = 1;
		} elseif ( $_SESSION['count'] > 3 ) {
			header("Location: baby/index.html");
			exit;
		} else {
			die ( login_screen($failed = 1) );
		}
	}
?>
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Thanks

Post by Mexican Hat »

Thanks for the code. I have one question. Where do I put the PHP file? In the cgi-bin or just with the rest of my site.

Peace 8)
User avatar
Slyvampy
Forum Newbie
Posts: 23
Joined: Thu Nov 28, 2002 2:03 am
Location: Yorkshire, England
Contact:

Post by Slyvampy »

The PHP page you are coding is the site. it replaces or complements the html pages.

HTML pages are static, PHP are server side scripting pages and are dynamic.

ie. you would put this code at the top of your page, rename the extention on your page
.php
instead of
.html or .htm
the output would be the html code.

if you need advice at any time, email me. (webmaster@slyvampy.com)

Cheers,

SteJ.
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Ok I understand Now

Post by Mexican Hat »

Ok I understand now. I just put my html in the PHP script. What I don't understand is how to make the username and passwords. I'm going to be the one who puts the name and passwords for people. I don't want it where is does it for them. Do the username and passwords go in the php file some where.

Peace 8)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

at the top of the login.php script i put above, change

function valid_login($username, $password) {
return ($username == 'elipollak' && $password == 'pass');
}

to your own username and password instead of 'elipollak' and 'pass' respectively

cheers
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Post by Mexican Hat »

If I wanted to make more username and passwords I would just copy this code:
function valid_login($username, $password) {
return ($username == 'elipollak' && $password == 'pass');
Peace 8)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

change it to


function valid_login($username, $password) {
return ($username == 'name1' && $password == 'pass1') or
($username == 'name2' && $password == 'pass2') or
($username == 'name3' && $password == 'pass3');
}
Post Reply