Newbie problem with a login...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Newbie problem with a login...

Post by Random »

I am just learning how to use sessions and stuff and I keep getting an 'error' you could say..here are the scripts (nothing too fancy):

index.php:

Code: Select all

<HTML>
<HEAD>
<TITLE>.: Random Design :..: Home :.</TITLE>
</HEAD>
<BODY bgcolor="White" text="Black" link="Navy" alink="Fuchsia">
<p align="Center">
<? print "Welcome!  Please log in." ?>
<FORM action="process.php" method="POST">
Username:<input type="text" name="username" maxlength="15"><br>
Password:<input type="password" name="password" maxlength="10"><br>
<INPUT type="submit" value="Log in!">
</BODY>
</HTML>
process.php:

Code: Select all

<? session_start(); ?>
<? session_register("username","password"); ?>
<?php
if( !$_POST&#1111;'username'] ) &#123;
	print "No <b>username</b> entered!";
	die;
&#125;

if( !$_POST&#1111;'password'] ) &#123;
	print "No <b>password</b> entered!";
	die;
&#125;

else &#123;
	print "Thank you for logging in, <b>".$_POST&#1111;'username']."</b>!";
&#125;
?>
<br>
<? print "Goto the main website by clicking " ?>
<a href="index2.php">here!</a>
index2.php:

Code: Select all

<? session_start(); ?>
<? session_register("username","password"); ?>

<br>
<br>
<br>
<?php print "Currently logged in as: ".$username."!"; ?>
It gets to index2.php just fine, but when it hits there the 'error' as I mentioned is this:

Currently logged in as: !

how do I get it to not say that? Thanks for any help :) Again, I am not the greatest scripter inthe world, just started learning a few days ago.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

i see a few things wrong with your code. The answer to your main problem, always displaying "Loged in at !" is because you arent using $_SESSION when you print $username. so

Code: Select all

echo "Currently loged in as " . $_SESSION['username'] . "!";
but on process.php, you have two logic errors. first, you need to use empty() to check if data was entered in a form.

Code: Select all

if(empty($_POST['username'])) { 
die("username is required");
}
and because you have the two if() statements seperate, then the else() is useless. I sudjest you do something like this:

Code: Select all

if(empty($_POST['username']) || empty($_POST['password'])) {
die("all fields are required");
} else {
echo "You have loged in";
}
now, i don't use sessions, but i think that in process.php, you need to do something like...

Code: Select all

session_register("$_POST['username']");
session_register("$_POST['password']");
im not sure though.
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

okay thank you very much, I shall surely try this out and post if i get another problem. I am trying to make a site like http://www.neopets.com or something of that sort. Thanks again. :D
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

you said you dont do sessions...how do u do stuff like forums, etc, without having a session? thanks in advance.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can't actually use session_register() and $_SESSION together.

Try:

Code: Select all

<?php
session_start();

if(empty($_POST['username']) || empty($_POST['password'])) { 
    die('all fields are required'); 
} else { 
    echo 'You have logged in';
    $_SESSION['username'] = $_POST['username'];
}

echo '<p>Goto the main website by clicking <a href="index2.php">here!</a></p>';
?>
and

Code: Select all

<?php
session_start();

echo '<p>Currently logged in as: '.$_SESSION['username'].'!</p>';
?>
Although you are not actually authenticating anything - just checking that they filled something in the fields for username and password.

You did read this didn't you?
viewtopic.php?t=6521

Mac
Post Reply