Login Script with Sessions

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
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Login Script with Sessions

Post by Kingo »

I'm trying to make a login page with sessions. I'm not understanding where the problem is. ANy help is really appreciated.

Code: Select all

<?php
//if they haven't pressed the submit button, then show the form
if(!$submit)
{
echo "<html><head><title>My Login Form</title></head><body>";
echo "<form name="l1" action=$_SERVER[PHP_SELF] method="post">";
echo "<div>Username: <input type="text" name="username" /><br />";
echo "Password: <input type="password" name="password" /><br />";
echo "<input type="submit" name="submit" value="Login" /><br />";
echo "</div></form></body></html>";

}

else //otherwise, let's process this stuff
{
if($username == "user" && $password == "mypass") //if they got it right, let's go on
{
session_start();
session_register("mysessionvariable"); //set a variable for use later
$id = session_id(); //let's grab the session ID for those who don't have cookies
$url = "Location: page2.php?sid=" . $id;
header($url);
}
/*else //they got something wrong and we should tell them
{


echo "<html><head><title>My Login Form</title></head><body>";
echo "<span style="color:#ff0000;">Password/Username Is Invalid</span><br />";
echo "<form action="<?$PHP_SELF?>" method="post">";
echo "<div>Username: <input type="text" name="username" /><br />";
echo "Password: <input type="password" name="password" /><br />";
echo "<input type="submit" name="submit" value="Login" /><br /></div></form>";
echo "</body></html>";

}*/
}
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

lookup "register globals" problems....
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I had set the register_globals ON in php.config file which is localed in "C:\PHP\BACKUP folder. But still it doesnt work. Please help. I'm working on this on my localmachine with IIS being configured with PHP 4
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

but you did not reload/restart your webserver.. and therefore the new settings were not loaded..... because your code seems to work (when i turn on register_globals)
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

It doesnt work and I even get the following notice.

Notice: Undefined variable: submit in C:\Inetpub\wwwroot\PHP_Remote\z1.php on line 3

Please help . When I click the submit button, nothingh is happening
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php


if (isSet($_POST['submit'])) { // if theyre trying to log on

    if ($_POST['username'] == 'user' && $_POST['password'] == 'pass') {

        session_start();
        $_SESSION['logged_in'] = true; // dont need to use session_register no more, just make variables like that at anytime after session_start();
        header("Location: page2.php?sid=" . session_id());

    } else {
        echo 'bad user or pass';
    }

} else { // show login form

// drop out of php to make your coding easier
?> 
<html><head><title>My Login Form</title></head><body>
<span style="color:#ff0000;">Password/Username Is Invalid</span><br />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div>Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" /><br /></div></form>
</body></html>
<?php
}


?>

<?php

// also, learn to use single quotes, makes life easier. Should only use double quotes when you need to

echo '

<html><head><title>My Login Form</title></head><body>
<span style="color:#ff0000;">Password/Username Is Invalid</span><br />
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
<div>Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" name="submit" value="Login" /><br /></div></form>
</body></html>


';

?>

and then on the page2.php

Code: Select all

<?php

session_start();

if (isSet($_SESSION['logged_in'])) {
    // all is well
} else {
    echo 'not logged in';
}

?>
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much . It is working. One more small question. What code should I write on all the pages to maintain the session of the logged in user.
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I used

Code: Select all

<?php session_start(); ?>
in one of the pages that is linked from the page2.php . I get the following warnings
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Inetpub\wwwroot\PHP_Remote\v4.php:8) in C:\Inetpub\wwwroot\PHP_Remote\v4.php on line 9

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Inetpub\wwwroot\PHP_Remote\v4.php:8) in C:\Inetpub\wwwroot\PHP_Remote\v4.php on line 9

I donot know what i have done is correct or not. Please help.
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I put the session_start(); in the beginning and the warnings are gone. But still I'm not able to access the user name on all the pages.
I used the following on all the pages and I'm getting errors
echo "Welcome $user"; on all the pages. I donot know how to maintain the session. Please help
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

u really need to read some session scripts or do some session tutorials.

put this in the above of your page and see if the session variables are present

<pre>
print_r($_SESSION);
</pre>
Post Reply