View Full Version : User Login System

12-19-2007, 07:15 PM

I've only just recently crossed the bridge from total n00b to "marginally competent" with PHP scripting, so please bear with me.

I'm working on a user login system and i'm struggling with one of the concepts.

I have a sidebar which will show a username and password form if there's no session variable set, but if the session variable is set, I want it to say "Welcome back George Bush or whoever the username is.

Once a user logs in, I want the same page displayed that they were browsing, but with the updated sidebar.

How do I do that? I don't know what .php file to to point the form action to.

Any help would be greatly appreciated!



Eternity Angel
12-19-2007, 08:44 PM
You would probably track the current page in a hidden form element for each page load, then when the login was a success, simply header("Location: ".$storedpage) them to the last page they were on. You can use $_SERVER['PHP_SELF'] for which page they are on.

12-19-2007, 11:45 PM
Fantastic, that's simpler than I thought!

And how about my sidebar? I've currently got a submit_reg.php which actually does the login and sets session stuff, with the login box on the sidebar, where would you have the form action pointing to?

Eternity Angel
12-19-2007, 11:53 PM
Actually, you CAN just point it ANYWHERE the page is located, as long as you include the submit_reg.php page on every page.

12-19-2007, 11:54 PM
Could I have the sidebar take care of it all for me, as that is included on every page?

Eternity Angel
12-19-2007, 11:55 PM
Sure! As long as it checks for the $_POST data, and sets the session, it can be anywhere. :-)

12-19-2007, 11:57 PM
Great, thanks very much for helping me out :p

Eternity Angel
12-19-2007, 11:57 PM
You're welcome. :-)

12-20-2007, 12:16 AM
Works! I just did this:

<form name="register" method="post" action="' . $_SERVER['PHP_SELF'] . '">

12-20-2007, 01:06 AM
I'm ALMOST there...

Upon login it processes one piece of code and logs the user on perfectly, but I want it to also display what's in the else statement.

<div id="container">
<div id="sidebar1">
include 'database.php';
include 'functions.php';
if(strstr($_SERVER['PHP_SELF'], "register.php")) {
echo "<p>Please Register</p>";
elseif($_GET['logoff'] == "yes") {
/////////////////// - This code is processed on login - //////////////////////
elseif(isset($_POST['login'])) {
user_login($_POST['username'], $_POST['password']);
echo "LOGIN FORM";
echo '
<p>Please log in:</p>
<form name="register" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<p>Username:<input type="text" name="username" value="';if(isset($_POST['username'])) { echo $_POST['username']; } echo '"/>Password:
<input type="password" name="password"/>
<input type="submit" value="Log in"/>
<input type="hidden" name="login" value="TRUE"/>
<br />
<span class="style1"><a href="register.php">Register</a></span></p>
/////////////////// - I Want this part displayed on login - //////////////////////
else {
echo '<p>Welcome back ' . $_SESSION['username'] . '</p>
<a href="' . $_SERVER['PHP_SELF'] . '?logoff=yes">Log Off</a><form><input type="hidden" name="logoff" value=TRUE/></form>';
<p class="style1">&nbsp;</p>
<!-- end #sidebar1 --></div>

<div id="mainContent">
<h1 align="right"><a href="index.php">aquabook</a></h1>

How can I make the welcome message in the else statement be displayed?

Can I do a GOTO of some description?

Eternity Angel
12-20-2007, 06:32 AM
Very simple, don't put the "!is_authed()" bit as an elseif... just put it as an if, then it will check it every time, regardless of if the user is logged in or not.

elseif (!is_authed()) { -->
if (!is_authed()) {

12-20-2007, 09:47 AM
Haha, that simple :)

It works perfectly now... woohoo! My first proper script :) Thanks a lot!

Eternity Angel
12-20-2007, 02:38 PM
You're welcome again. :-)