Click to See Complete Forum and Search --> : Loging in...


YourSurrogateGod
November 28th, 2004, 06:15 PM
Here's what I'm trying to do. I want to be able for the user to log into a webpage. I would like to get the username and password, compare them to existing ones in the database and then move that person along to the main web page, otherwise they will get an echo saying that they've screwed up. Also, once the login was succesful, 2 cookies get set so that they can be use later on in the webpage. I'm pretty confident that I've screwed this up (setcookie is supposed to go before <html> :rolleyes: ), any ideas on how I can get at what I'm trying to accomplish? How would I set a cookie after processing a specific form?

Thanks.<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>$Title</title>
</head>

<!-- Background color. -->
<body bgcolor="#FFFFFF" text="#000000">

<!-- The central title. -->
<p align="center"><font size="6">$Title</font></p>

<!-- For a new user. -->
<p align="center"><a href="newuser.php">I'm a New User</a></p>

<!-- Will indicate what action needs to be taken. -->
<form action="main.php" method="POST" name="LoginForm">
<!-- Align all of the values to the center. -->
<p align="center">Username:
<!-- Will allow the user to input his/her username. -->
<input type="text" size="50" name="Username"><br>
Password:
<!-- Will allow the user to input his/her password. -->
<input type="password" size="50" name="Password"><br>
<!-- Will display an error message if the password or username are not correct. -->
<font color="#FF0000">$ErrorMessage</font><br>
<!-- Will hold the button that will need to be pressed. -->
<input type="submit" name="Submit" value="Login"></p>
<?php
$_POST('Password');
$_POST('Username');

// Attempt to connect to the database.
$db = mysql_pconnect("localhost", "root", "255sql");

// Make sure the DB was opened.
if(!$db)
{
echo "Error: Failed to open database.";
exit;
}

mysql_select_db("cse255", $db);

if(!isset($_POST['submit']))
{
$_POST['submit'];
}
else
{
$tResult = mysql_query("SELECT FROM User_Ta WHERE UName = " .
$Username . " UPasswd = " . $Passwd .
" OR APasswd = " . $Passwd);

if(mysql_num_rows($tResult) == 0)
{
echo "ERROR: Wrong username or password.";
}
else
{
setcookie("uname", $Username);
setcookie("password", $Password);

include("main.php");
}
}
?>
</form>
</body>
</html>

YourSurrogateGod
November 28th, 2004, 07:27 PM
Err... I already have this one figured out.

khp
November 28th, 2004, 07:31 PM
How would I set a cookie after processing a specific form?


If you would read the documentation for setcookie, you can (as of PHP4) use output buffering to be able to use setcookie anywhere in your script. Alternativly you could just do 'Manual output buffering' :sick: writing your output to a string, before setting your cookies. And printing said string after setting the cookies.

Although I don't understand why you don't just move the logic for setting the cookies, up above the <html> tag. I would think this would make your sciript both cleaner better and faster.

http://www.php.net/manual/en/function.setcookie.php

YourSurrogateGod
November 28th, 2004, 08:22 PM
If you would read the documentation for setcookie, you can (as of PHP4) use output buffering to be able to use setcookie anywhere in your script. Alternativly you could just do 'Manual output buffering' :sick: writing your output to a string, before setting your cookies. And printing said string after setting the cookies.

Although I don't understand why you don't just move the logic for setting the cookies, up above the <html> tag. I would think this would make your sciript both cleaner better and faster.

http://www.php.net/manual/en/function.setcookie.phpThis is what I got at wih someone's help...<?php
$Passwd = addslashes($_POST['Password']);
$Username = addslashes($_POST['Username']);

// Attempt to connect to the database.
$db = mysql_pconnect("localhost", "root", "password") or DIE("Error: Failed to open database. " . mysql_error());
mysql_select_db("cse255", $db) or die("Can't select db " . mysql_error());

if (isset($_POST['submit'])) {
$tResult = mysql_query("SELECT FROM User_Ta WHERE UName = '$Username' AND UPasswd = '$Passwd' OR APasswd = '$Passwd';");

if (!mysql_num_rows($tResult)) {
$ErrorMessage = "ERROR: Wrong username or password.";
} else {
setcookie("uname", $Username);
setcookie("password", $Password);
include("main.php");
exit;
}
} else {

echo <<<HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>$Title</title>
</head>

<!-- Background color. -->
<body bgcolor="#FFFFFF" text="#000000">

<!-- The central title. -->
<p align="center"><font size="6">$Title</font></p>

<!-- For a new user. -->
<p align="center"><a href="newuser.php">I'm a New User</a></p>

<!-- Will indicate what action needs to be taken. -->
<form action="main.php" method="POST" name="LoginForm">
<!-- Align all of the values to the center. -->
<p align="center">Username:
<!-- Will allow the user to input his/her username. -->
<input type="text" size="50" name="Username"><br>
Password:
<!-- Will allow the user to input his/her password. -->
<input type="password" size="50" name="Password"><br>
<!-- Will display an error message if the password or username are not correct. -->
<font color="#FF0000">$ErrorMessage</font><br>
<!-- Will hold the button that will need to be pressed. -->
<input type="submit" name="Submit" value="Login"></p>
</form>
</body>
</html>
HTML;
}
?>