Page 1 of 1
Login and Password with PHP
Posted: Sat Mar 08, 2003 8:35 pm
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

Posted: Sat Mar 08, 2003 10:01 pm
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) );
}
}
?>
Thanks
Posted: Sat Mar 08, 2003 10:52 pm
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

Posted: Sat Mar 08, 2003 11:46 pm
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.
Ok I understand Now
Posted: Sat Mar 08, 2003 11:55 pm
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

Posted: Sun Mar 09, 2003 3:05 am
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
Posted: Sun Mar 09, 2003 1:52 pm
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

Posted: Sun Mar 09, 2003 4:32 pm
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');
}