Page 1 of 1
Help with a login script
Posted: Mon Feb 17, 2003 4:32 pm
by Aero
Hello,
I just started using PHP and MYSQl a little while ago and now i want to make a login in script on every page that i have in like the menu, i can do that but i want it to disapear after i have logged in, and memeber stuff to come up, iv tried using if statements but i dont know what to do.
My code is below:
Code: Select all
<form method=post action="<?echo $PHP_SELF?>">
<table cellpadding=2 cellspacing=0 border=0>
<td>Username:</td><td><input type="text" name="username" size=10></td><tr>
<td>Password:</td><td><input type="password" name="password" size=10></td><tr>
<td> </td><td><input type="submit" name="submit" value="Log In"></td>
</table></form>
<?
if ($submit) {
$user_name=$_POSTї'username'];
$password=$_POSTї'password'];
$db=mysql_connect("localhost","root") or die ("Unable to connect.");
mysql_select_db("main",$db) or die ("Wrong Database information or error in coding.");
$query="Select * from Userbase where Account='$username' AND password='$password'";
$result=mysql_query($query, $db) or die ('ERROR');
$affected_rows=mysql_num_rows($result);
if ($affected_rows == 1) {
echo "You logged in!";
$_SESSIONї'username'] = $username;
} else {
echo ".";
}
}
?>
What would i do to make that stuff disapear after login, and only appear if ur not logged in, and how would i make stuff appear? Any help is greatly appreciated.
Posted: Mon Feb 17, 2003 6:38 pm
by lazy_yogi
u would need sessions for that
here's the script I use.
on every page at the very top before ANYTHING else , put
then in that directory
put this file and call it login.php
Code: Select all
<?php
// chang uname to ur username and pword to ur password here
function valid_login($username, $password) {
if ($username == 'uname' && $password == 'pword') return true;
else return false;
}
function create_form() {
$body = "<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>";
return $body;
}
session_start();
if(!isset($_SESSION['count'])) { $_SESSION['count'] = 0;}
$state = $_REQUEST["state"];
if(! $_SESSION['logged_in']) {
if (isset($_POST['submit'])) {
if(valid_login($_POST['username'], $_POST['password'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['count'] = 1;
$_SESSION['logged_in'] = 1;
} else {
$_SESSION['count']++;
$body = create_form()."<BR><BR><font color = red><b>Failed ".$_SESSION['count']." time(s) : Keep Trying Biatch</b></font><BR>";
$head = "<HTML><HEAD><LINK REL=StyleSheet HREF="style.css" TYPE="text/css"></HEAD>";
print $head."<BODY bgcolor=#e1e1e1><center>". $body. "<BR>".$body2. "<p><p></center></body></html>";
exit;
}
} else {
if ($_SESSION['count'] > 3) {
header("Location: baby/index.html");
exit;
}
$head = "<HTML><HEAD><LINK REL=StyleSheet HREF="style.css" TYPE="text/css"></HEAD>";
print $head."<BODY bgcolor=#e1e1e1><center>".create_form(). "<p><p></center></body></html>";
exit;
}
}
// $body = "Hello <em>".$_SESSION['username']."</em>, you have visited this site ".$_SESSION['count']." time(s)...";
// print "<html><head></head><body><center>". $body. "<p><p></center></body></html>";
$_SESSION['count']++;
?>
this can also work in other directories
eg in directory "newdir" under this directory containing the info
just do
Code: Select all
<?php include 'newdir/login.php'; ?>
instead of
Posted: Mon Feb 17, 2003 8:40 pm
by Aero
i would really like the login page to have more stuff on it, how do i add stuff b4 it w/o scewing up stuff?
and lets say once i have the session running and im on a different page, can i set up an if statement with a varible from the login page so that it shows something else?
im having trouble setting up the username and password thing to access the database...
Posted: Mon Feb 17, 2003 9:29 pm
by lazy_yogi
It can be modified for that.
Code: Select all
function valid_login($username, $password) {
// check username and password here
}
session_start();
if(!isset($_SESSION['count'])) { $_SESSION['count'] = 0;}
if(! $_SESSION['logged_in']) {
if (isset($_POST['submit'])) {
if(valid_login($_POST['username'], $_POST['password'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['logged_in'] = 1;
$logged_in = 1;
}
}
}
$logged_in = $_SESSION['logged_in'];
and then in your webpage
include this at the top
and u can tell if they are logged in via
if ($logged_in) .....
else ....
Posted: Mon Feb 17, 2003 9:46 pm
by Aero
well thanks for the help but now im so confused, lol.
i still dont really know alot about php, but i just need a stupid login script lol that will show stuff and i dont know how to do it, thanks for the help.
Posted: Mon Feb 17, 2003 10:18 pm
by lazy_yogi
its not difficult
just use the code above and put it in a file called login.php
in all other files put this at the first line of YOUR pages
and then next in YOUR pages, use
Code: Select all
if ($logged_in) {
// print stuff for logged in users here
} else {....
// print stuff for those not logged in
}
oh and you'll have to edit this function to check for username and password
Code: Select all
function valid_login($username, $password) {
// check username and password here
}