For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > April 2004 > Re: [PHP-DB] login script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] login script
Marcjon Louwersheimer

2004-04-27, 12:21 am

This is kind of a big thing. Email me and I'll give you instructions/tips
on making it.


----- Original message -----
From: "andy amol" <andy_amol_2003@yahoo.com>
To: "php php" <php-db@lists.php.net>
Date: Mon, 26 Apr 2004 17:15:27 -0700 (PDT)
Subject: [PHP-DB] login script

hi,
does anyone have a login script which will take data from the table and
decide whether the given user is a admin or a normal user.
thanks in advance.

Also if there is some help on session variable I would like to know.


---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
--
Marcjon
Mikael grön

2004-04-27, 4:50 am

Here you go:


Table:

ID Login PW Admin
1 admin abc123 1
2 normal bcd234 0
3 normal2 cde345 0


PHP:

[BOF]
<?php
if($_POST['login']) {
$result = mysql_query("select * from users where login = '" .
$_POST['login'] . "'") or die(mysql_error());
if (mysql_num_rows($result)) {
$data = mysql_fetch_array($result);
if ($data[2] == $_POST['pw']) {
$_SESSION['login'] = $data[1];
if ($data[3]) {
$_SESSION['admin'] = true;
}
header("Location: logged_in.php");
exit;
} else {
$error = "Wrong password";
}
} else {
$error = "No such user!";
}
}
if ($error) {
echo "<span class=\"loginError\">$error</span><br />";
}
?>
<html>
<head>
<title>Login Script by emgee@cwazy.co.uk</title>
<style>
<!--
..error {
color: #ff0000;
}
-->
</style>
<head>
<body bgcolor="#FFFFFF">
<form action="login.php" method="post">
Login name: <input type="text" name="login" value="<?php echo
$_POST['login'] ?>" /><br />
Password: <input type="password" name="pw" /><br />
<input type="submit" value="Login >" /><br />
</form>
</body>
</html>
[EOF]


In your admin system, just check for the variable $_SESSION['admin'].
If true, the user is admin and can do stuff.. ;)
I don't care about just having given you a lot of script, since I write
at least 3 of these per w.
And, make sure there is nothing echoed either by PHP or HTML before the
login script (or else the redirecting upon successful login won't work)

Regards, Mike


On Apr 27, 2004, at 01:15, andy amol wrote:

> hi,
> does anyone have a login script which will take data from the table
> and decide whether the given user is a admin or a normal user.
> thanks in advance.
>
> Also if there is some help on session variable I would like to know.
>
>
> ---------------------------------
> Do you Yahoo!?
> Win a $20,000 Career Makeover at Yahoo! HotJobs

Mikael grön

2004-04-27, 4:51 am

Err...

As usual I forgot the most important part...
Add

session_start()

just above the

if($_POST['login']) {

row, or it won't work at all.. ;)
Or rather, it'll work, but no sessions will be saved.

Mike


On Apr 27, 2004, at 08:48, Mikael Gr=F6n wrote:

> Here you go:
>
>
> Table:
>
> ID Login PW Admin
> 1 admin abc123 1
> 2 normal bcd234 0
> 3 normal2 cde345 0
>
>
> PHP:
>
> [BOF]
> <?php
> if($_POST['login']) {
> $result =3D mysql_query("select * from users where login =

=3D '" .=20
> $_POST['login'] . "'") or die(mysql_error());
> if (mysql_num_rows($result)) {
> $data =3D mysql_fetch_array($result);
> if ($data[2] =3D=3D $_POST['pw']) {
> $_SESSION['login'] =3D $data[1];
> if ($data[3]) {
> $_SESSION['admin'] =3D true;
> }
> header("Location: logged_in.php");
> exit;
> } else {
> $error =3D "Wrong password";
> }
> } else {
> $error =3D "No such user!";
> }
> }
> if ($error) {
> echo "<span class=3D\"loginError\">$error</span><br />";
> }
> ?>
> <html>
> <head>
> <title>Login Script by emgee@cwazy.co.uk</title>
> <style>
> <!--
> .error {
> color: #ff0000;
> }
> -->
> </style>
> <head>
> <body bgcolor=3D"#FFFFFF">
> <form action=3D"login.php" method=3D"post">
> Login name: <input type=3D"text" name=3D"login" value=3D"<?php echo=20
> $_POST['login'] ?>" /><br />
> Password: <input type=3D"password" name=3D"pw" /><br />
> <input type=3D"submit" value=3D"Login >" /><br />
> </form>
> </body>
> </html>
> [EOF]
>
>
> In your admin system, just check for the variable $_SESSION['admin'].=20=


> If true, the user is admin and can do stuff.. ;)
> I don't care about just having given you a lot of script, since I=20
> write at least 3 of these per w.
> And, make sure there is nothing echoed either by PHP or HTML before=20
> the login script (or else the redirecting upon successful login won't=20=


> work)
>
> Regards, Mike
>
>
> On Apr 27, 2004, at 01:15, andy amol wrote:
>
[color=darkred]
>
> --=20
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com