Web Hosting Talk







View Full Version : PHP HELP -- Is there anyone generous enough out there to make a login script for me?


All-Starr 24-7
08-22-2004, 06:57 PM
Hey everyone!I need a php/mysql login script, that uses cookies, and fetches the username/password from a mysql database!If you can make me this login script, please do and let me no!Thx!

Dan L
08-22-2004, 07:03 PM
Learn yourself! People are much more willing to help if you want to learn. :)

EXOWorks
08-22-2004, 07:46 PM
Read up some tutorials, its is pretty easy to make a script like that... :D

All-Starr 24-7
08-22-2004, 07:59 PM
K..here is the code i was trying, its two different files.

login.php
<form method="post" action="logGo.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login"></form>



This file is all html..just the form..


logGo.php
<?
require 'connect.php';
$query = "SELECT username,password
FROM users";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
if($username != $r[username] AND $password != $r[password]){
echo "Sorry, but your username and/or password is incorrect!";
}
else
{
setcookie ("loggedin",$username, time()+3600);
echo "Welcome $username !You have logged in to tutorialit.com";
}
}
?>



One of the probelms is that everything, such as the error message i have, and the message that welcomes the user of the login is correct,is "doubled".It seems to be output twice...Also, if the login information is correct, i get this error:

Error in logGo.php
Warning: Cannot modify header information - headers already sent by (output started at /home/neomania/public_html/connect.php:13) in /home/neomania/public_html/logGo.php on line 13
Welcome Xominik !You have logged in to tutorialit.com


This error message is with the cookie..and is also doubled as well.What can i do to fix the doubling problem, and the cookie error?

EXOWorks
08-22-2004, 08:04 PM
Below <? add:

ob_start();

Before ?> add:

ob_end_flush();

Dan L
08-22-2004, 08:13 PM
I do something like..


<?php
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
$query = mysql_query("select * from `members` where user={$username}");
$r = mysql_fetch_array($query);
if($password != $r['password'])
{
die('Incorrect.');
}
else
{
echo 'Success!';
}
?>


Not so sure about the cookie thing, though.

All-Starr 24-7
08-22-2004, 08:14 PM
Thx!It got rid of the cookie error!For the doubling, ill just use the "die" function!Thank you!

Burhan
08-23-2004, 02:46 AM
Your script still won't work.

Replace $password with $_POST['password'] and $username with $_POST['username']. Read this (http://www.php.net/register_globals) to find out why.

You can also have a look at this tutorial (http://www.zend.com/zend/tut/authentication.php) which shows you different ways of doing authentication.

Tomer
08-23-2004, 07:27 AM
All-Starr 24-7, I suggest something simpler like this.


File1.php

<?php

<form action = 'File2.php' method = 'POST'>

Username: <input type = 'text' name = 'username' size='32'>
Password: <input type = 'password' name = 'password' size='32'>

<input type = 'submit' value = 'Login!'>
</form>
?>



File2.php

<?php

$user = $_POST[username];
$pass = $_POST[username];

// Connect to database.

$query = "SELECT *
FROM Members
WHERE username = '{$user}'
AND password = '{$pass}'
LIMIT 1";

$result = mysql_query($query)
or die ("Couldn't execute result.");

$row = mysql_fetch_array($result,MYSQL_ASSOC);
@ extract($row);

if ($row[username] != "")
{
$_SESSION['login'] = "yes";
echo "$row[username], you have been logged in successfully!";
}

else
{
echo "Sorry, it seems your username/password can't be verifyed. <br />
Please try again.";
}


?>

Burhan
08-23-2004, 07:51 AM
I think this needs to be fixed :

$user = $_POST[username];
$pass = $_POST[username];

:)

Tomer
08-23-2004, 08:36 AM
Haha thanks. :D

Can someone edit it?

Burhan
08-24-2004, 02:30 AM
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$username = stripslashes($_POST['username']);
$pass = stripslashes($_POST['password']);

if ($username == "" || $pass == "") { die("Sorry, you need to fill both fields"); }
if (!mysql_connect("localhost","username","secret")) { die(mysql_error()); }
if (!mysql_select_db("userdb")) { die(mysql_error()); }
$query = "SELECT username FROM `users` WHERE username = '".mysql_escape_string($username)."' AND pass = '".mysql_escape_string($pass)."' LIMIT 1";
$res = mysql_query($query);
if (!$res) { die($query."<br />".mysql_error()); }
if (mysql_num_rows($res) > 0)
{
session_start();
$_SESSION['username'] = $username;
echo "Thank you, you are authorized";
} else {
echo "Sorry, invalid credentials";
}
} else {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" />
</form>
<? } ?>

emfr
08-24-2004, 09:24 AM
go on phpfreaks.com there is a very nice tutorial there!