Forum Moderators: coopster

Message Too Old, No Replies

Session problems!

         

toltec75

7:28 pm on Sep 6, 2004 (gmt 0)

10+ Year Member



I`m using user authentication against database driven values!
After the user has been verified I register Username and password as session variables!
The form looks like this!

<?php
session_start();

if ((!$_SESSION[username]) ¦¦ (!$_SESSION[password])) {

echo"
<HTML>
<HEAD>
<TITLE>My Login Form></TITLE>
</HEAD>

<FORM ACTION=\"login1.php\" METHOD=\"post\">

<table border=0>
<tr>
<td><strong>User name:</strong></td>
<td><input type=\"text\" name=\"username\"></td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td><input type=\"password\" name=\"password\"></td>
</tr>
<tr>
<td>
<input type=\"submit\" value=\"Validate Me\">
</td>
</tr>
</table>
</FORM>
</BODY>
</HTML>";
}
else
{ header("Location: menu.php");
}
?>

and in processing script I register username and password:-

<?php
session_start();

$conn = mysql_connect("localhost", "root") or die(mysql_error());

$db = mysql_select_db("database", $conn) or die(mysql_error());

$sql = "SELECT id FROM users WHERE user='$_POST[username]' and password=
'$_POST[password]'";

$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array ( $result );

$id = $row["id"];
$username=$_POST["username"];
$password=$_POST["password"];

if (mysql_num_rows($result) == 1) {

session_register("id");
session_register("username");
session_register("password");

header("Location: menu.php");

}
else {
echo "You`re not authorized to see this page!";
echo "<p><a href=form.php>Please login!</a></p>";
}

?>

I carry on username and password as session variables and it works fine unless the PHP settings are set to display all warnings and errors!

Then after starting form.php (the most upper script)I get:

Notice: Use of undefined constant username - assumed 'username' in c:\apache\htdocs\iwa2003_12\form.php on line 4

Notice: Use of undefined constant password - assumed 'password' in c:\apache\htdocs\iwa2003_12\form.php on line 4

How do I get rid of this! It`s very important!
If my authentication system is bad could you suggest something else?

ergophobe

7:40 pm on Sep 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



YOu have forgotten to quote your array indexes. Try this

if ((!$_SESSION['username']) ¦¦ (!$_SESSION['password']))

and it should fix it right up

Also, instead of session_register() use the superglobals. Instead of

session_register("username");

try this

$_SESSION['username'] = $username;

Tom

toltec75

8:04 pm on Sep 6, 2004 (gmt 0)

10+ Year Member



Wow, what a stupid mistake by me! Thanx! ;)
I still get:

Notice: Undefined index: username in c:\apache\htdocs\iwa2003_12\form.php on line 4

but after logging in everything is OK now!

I get that message when starting form.php or other files that require aunthentication and are directed to form.php!

I already used $_SESSION... but it did the same thing!

mincklerstraat

8:11 pm on Sep 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ((empty$_SESSION['username']) ¦¦ (empty$_SESSION['password'])) {

nb.: there are forms of punctuation in the English language other than the exclamation point!

;)!

toltec75

8:21 pm on Sep 6, 2004 (gmt 0)

10+ Year Member



Actually it`s more like this ;)

if ((empty ($_SESSION['username'])) ¦¦ (empty ($_SESSION['password']))) {

I just combined your code a bit and came to this!
Everythig is OK now!

Thanx man, you`re a true saviour!