|
-
Feb 10th, 2013, 06:20 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] login problem
hello guys,
i'm having a problem again with my php code.
when i login to my site my login box is ment to disappear when i login and show my username and a link to the login page but it isnt doing that it keeps showing the login box can anyone help me please?.
you can see what i mean if u login using
mark
d82f1fc
when they login it should show this part of the code.
PHP Code:
if ($username){ echo "$username | <a href='logout.php'>Logout</a>"; }
if they arent logged in it should show
PHP Code:
echo "<form action='login.php' method='post'> <center><table> <tr> <td>Username</td> <td><input type='text' name='username' class='textbox' size='35'></td> <td><a href='register.php'>Register</a></td> </tr> <tr> <td>Password</td> <td><input type='password' name='password' class='textbox' size='35'></td> <td><input type='submit' name='loginbtn' value='Login' class='button'></td> </tr> </table></center> </form>";
This is the full code below.
PHP Code:
<?php
if ($username){ echo "$username | <a href='logout.php'>Logout</a>"; } else echo "<form action='login.php' method='post'> <center><table> <tr> <td>Username</td> <td><input type='text' name='username' class='textbox' size='35'></td> <td><a href='register.php'>Register</a></td> </tr> <tr> <td>Password</td> <td><input type='password' name='password' class='textbox' size='35'></td> <td><input type='submit' name='loginbtn' value='Login' class='button'></td> </tr> </table></center> </form>";
?>
Last edited by Jamie_Garland; Feb 10th, 2013 at 06:26 PM.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Feb 10th, 2013, 07:38 PM
#2
Re: login problem
Is that all there is? Where is $username getting set? Presumably login.php checks the user name and password, and the sets a cookie, which should then be used to populate $username...
-tg
-
Feb 11th, 2013, 08:12 AM
#3
Thread Starter
Frenzied Member
Re: login problem
hello thanks for the reply.
this is the code for the login.php code.
PHP Code:
<?php
$title = "MBAPPZ.com - Login"; ?>
<?php require("styles/top.php"); ?>
<div id='full'>
<?php
$form = "<form action='login.php' method='post'>
<center><table>
<tr>
<td>Username</td>
<td><input type='text' name='username' class='textbox' size='35'></td>
<td><a href='register.php'>Register</a></td>
</tr>
<tr>
<td>Password</td>
<td><input type='password' name='password' class='textbox' size='35'></td>
<td><input type='submit' name='loginbtn' value='Login' class='button'></td>
</tr>
</table></center>
</form>";
if ($_POST['loginbtn']){
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
if ($username && $password){
require("scripts/connect.php");
$pass = md5(md5($password));
$query = mysql_query("SELECT * FROM users WHERE username='$username' And password='$pass'");
$numrows = mysql_num_rows($query);
if ($numrows == 1) {
$row = mysql_fetch_assoc($query);
$dbid = $row['id'];
$dbuser = $row['username'];
$_SESSION['user'] = $dbuser;
$_SESSION['userid'] = $dbid;
echo "<body onload=\"setTimeout('redirect();', 3000);\">";
echo "You have been logged in as <b>$dbuser</b>. You will now be redirected to our homepage in a few seconds";
}
else
echo "Your login information was incorrect. $form";
}
else
echo "You did not fill in the entire form. $form";
}
else
echo "$form";
?>
</div>
<?php require("styles/bottom.php"); ?>
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Feb 11th, 2013, 08:41 AM
#4
Re: login problem
OK... so I see you're setting the session object...
$_SESSION['user'] = $dbuser;
$_SESSION['userid'] = $dbid;
But are you reading it back on subsequent pages?
At the top of each page, you need to read back the session variables to see if you have a User name and User ID... then display (or not) the login form again (or not)...
-tg
-
Feb 11th, 2013, 08:49 AM
#5
Thread Starter
Frenzied Member
Re: login problem
hello,
at the top of the top.php page i have
<?php
session_start();
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
?>
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Feb 11th, 2013, 08:53 AM
#6
Re: login problem
Is the re-direct working following a login?
-tg
-
Feb 11th, 2013, 09:00 AM
#7
Thread Starter
Frenzied Member
Re: login problem
yeah it redirects me to the page but it should show this if logged in
echo "$username | <a href='logout.php'>Logout</a>";
and show the form after logout or not logged in?.
Last edited by Jamie_Garland; Feb 11th, 2013 at 09:06 AM.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Feb 11th, 2013, 09:18 AM
#8
Re: login problem
Ha!... found it....
When you set the session var.... you used this:
$_SESSION['user'] = $dbuser;
$_SESSION['userid'] = $dbid;
but when you read it back you use this:
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
See the difference yet?
$_SESSION['user'] = $dbuser;
$username = $_SESSION['username'];
you're writing to one session var, but reading back from another... and so $username is empty and your code thinks the user isn't logged in.
-tg
-
Feb 11th, 2013, 09:22 AM
#9
Thread Starter
Frenzied Member
Re: login problem
I changed the top to $username = $_SESSION['user'];
but noe the redirect code isnt working?.
Last edited by Jamie_Garland; Feb 11th, 2013 at 09:36 AM.
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
-
Feb 12th, 2013, 04:43 PM
#10
Frenzied Member
Re: login problem
Looking at what code you provided for login.php, you didn't call session_start(). You have to call this on every page that you intend to set or retrieve $_SESSION variables.
Otherwise, if you're getting redirected (meaning your login was successful), $_SESSION['user'] and ['userid'] should work.
-
Feb 12th, 2013, 07:37 PM
#11
Re: login problem
 Originally Posted by MonkOFox
Looking at what code you provided for login.php, you didn't call session_start(). You have to call this on every page that you intend to set or retrieve $_SESSION variables.
Otherwise, if you're getting redirected (meaning your login was successful), $_SESSION['user'] and ['userid'] should work.
I thought the same thing too... but it's in top.php...
 Originally Posted by Jamie_Garland
hello,
at the top of the top.php page i have
<?php
session_start();
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
?>
if all pages include top.php, then the session should be started, and he's able to read the session data...
-tg
-
Feb 13th, 2013, 08:28 AM
#12
Frenzied Member
Re: login problem
Ah, didn't see that. Hmm, well in that case I'm not really sure what's going on.
-
Feb 14th, 2013, 08:52 AM
#13
Thread Starter
Frenzied Member
Re: login problem
hello,
i got this part of the code sorted.
I was wondering is it possible for me if i login to the page with the username Mark it shows a new link saying Add Tutorials but if they arent Mark is dosent show the link can this be done?.
This is the code i have so far?.
<?php
if ($username = 'Mark'){
echo "<a href='profile.php?id=$userid'>$username</a> | <a href='logout.php'>Logout</a>";
}
else
echo "<form action='login.php' method='post'>
<center><table>
<tr>
<td>Username</td>
<td><input type='text' name='username' class='textbox' size='25'></td>
<td><a href='register.php'>Register</a></td>
</tr>
<tr>
<td>Password</td>
<td><input type='password' name='password' class='textbox' size='25'></td>
<td><input type='submit' name='loginbtn' value='Login' class='button'></td>
</tr>
</table></center>
</form>";
?>
come back and mark your original post as resoved if your problem is fixed
Jamie Garland
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|