Forum Moderators: coopster

Message Too Old, No Replies

managing user's access level in logins

         

lindaonline15

6:38 am on Sep 5, 2008 (gmt 0)

10+ Year Member



hello all. I am trying to make user's access levels for login of my pages.
I'm not sure the way im applying it is correct, if im wrong please tell me. the problem Im facing now is, after login nothing happens, no error, no page.

I've created a login page, members page, and checklogin page.

the code for login.php is:

<html>
<head>
<title>Login</title>
<head>
<body>

<?PHP
//Connect to database
include_once "connection.php";

if(isset($_POST['Login']))
{
if($_POST['username']!='' && $_POST['password']!='')
{
//Use the input username and password and check against 'users' table
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);

$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";

if(mysql_num_rows($query) == 1)
{
$row = mysql_fetch_assoc($query);
{
$_SESSION['username'] = $row['username'];
$_SESSION['logged_in'] = TRUE;
header("Location: members.php");
print("<h3>Welcome $username!</h3>");
include "members.php";
}

}
else {
$error = 'Login failed !';
}
}
else {
$error = 'Please user both your username and password to access your account';
}

}

?>

<?php if(isset($error)){ echo $error;}?>

</body>
</html>

***************************************************************************

after that, the members.php is:

<html><head><title>Feedbacks</title></head>

<body>

<?php
include_once "connection.php";

//this is where I check the access level
<?php
include "checklogin.php"
session_start();
checkLogin('0 1 2');
?>

?>
<?php

$query = "SELECT * FROM feedbacks";

.
.
.
"some codes that work well seperately..."
.
.
.
<br /><strong>
<?php print("$num") ?> feedback(s).</strong> <br/> <br />
<?php
mysql_close();
?>
</body>
</html>

***************************************************************************

and this is my checklogin.php:

<html>
<head />
<title />
<body>

<?php

include_once "connection.php";

function checkLogin($levels)
{
if(!$_SESSION['logged_in'])
{
$access = FALSE;
}
else {
$kt = split(' ', $levels);

$query = mysql_query('SELECT Access_lvl FROM users WHERE username = "'.mysql_real_escape_string($_SESSION['username']).'"');
$row = mysql_fetch_assoc($query);

$access = FALSE;

while(list($key,$val)=each($kt))
{
if($val==$row['Access_lvl'])
{//if the user level matches one of the allowed levels
$access = TRUE;
}
}
}
if($access==FALSE)
{
header("Location: login.php");
}
else {
//do nothing: continue
}

}
?>

</body>
</html>

anyone can tell me what is wrong in here...? :(

dreamcatcher

7:35 am on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Linda,

You are trying to use the header [uk.php.net] function after you have already sent data to the browser. This will cause the header function to fail. You can get around it using output buffering. Or by moving your processing code to before the HTML code.

From the PHP website:

<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>

Another useful thing is to always set your error reporting to E_ALL for development, this will give you some debugging clues.

dc

lindaonline15

7:43 am on Sep 5, 2008 (gmt 0)

10+ Year Member



thx dreamcatcher for reply..
but im still blur, can you please suggest a way to do so?
thx

lindaonline15

9:02 am on Sep 5, 2008 (gmt 0)

10+ Year Member



I tried that also.. still the same problem..

lindaonline15

2:16 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



anyone can help me with this? is kind of important..

jatar_k

2:29 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



can you explain exactly what's happening, or not happening

you go to the login form
you enter correct user/pass
hit submit

what happens next?
what steps have you tried to try and diagnose the problem
does the url change? are we sure it is going to the next script or is it not submitting
have you dumped the $_POST data at the top of the login.php page like so

echo '<pre>';
print_r($_POST);
echo '</pre>';

what does it show?

lindaonline15

2:36 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



i have an html page for login, when i submit the user name, pass, the link will change to login.php with a blank page appearing.
shouldn't it show memebers.php?

i did what you just said, to echo the submittion
i got this:
Array
(
[username] => nanaz
[password] => nazanin
[submit] => Login
)

jatar_k

3:00 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so we now know that data is getting posted
we also know that it dies on login.php

we don't know why though

to start with this line needs to go

header("Location: members.php");

as dc mentioned, it won't work, it is also redundant since the rest is built to not be a redirect, the other issue is if the script was getting to that point there would be an error, there isn't so let's assume it isn't getting there

still take that line out never to be seen again though :)

I went through the code now

your braces are wrecked, try this

<html>
<head>
<title>Login</title>
<head>
<body>
<?PHP
//Connect to database
include_once "connection.php";
if(isset($_POST['Login'])) {
if($_POST['username']!='' && $_POST['password']!='') {
//Use the input username and password and check against 'users' table
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
if(mysql_num_rows($query) == 1) {
$row = mysql_fetch_assoc($query);
$_SESSION['username'] = $row['username'];
$_SESSION['logged_in'] = TRUE;
print("<h3>Welcome $username!</h3>");
include "members.php";
} else {
$error = 'Login failed !';
}
} else {
$error = 'Please user both your username and password to access your account';
}
}
if(isset($error)){ echo $error;}
?>
</body>
</html>

lindaonline15

3:06 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



still the same... just directs to login.php

jatar_k

3:13 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



wel that did fix a couple brace issues and got rid of the header line so you need to work from that code

I really don't think your server is outputting errors because your first script should have thrown a couple parse errors

you should look at the value of display_errors [php.net] in your php.ini

lindaonline15

3:23 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



my display errors was off. I changed it to ON, but the situation is still just the same...

jatar_k

3:27 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



and when you view source is anything there?

you also aren't actually executing your query, you might want to add it

<html>
<head>
<title>Login</title>
<head>
<body>
<?PHP
//Connect to database
include_once "connection.php";
if(isset($_POST['Login'])) {
if($_POST['username']!='' && $_POST['password']!='') {
//Use the input username and password and check against 'users' table
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);
$q = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$query = mysql_query($q) or die("there was an error: " . mysql_error());
if(mysql_num_rows($query) == 1) {
$row = mysql_fetch_assoc($query);
$_SESSION['username'] = $row['username'];
$_SESSION['logged_in'] = TRUE;
print("<h3>Welcome $username!</h3>");
include "members.php";
} else {
$error = 'Login failed !';
}
} else {
$error = 'Please user both your username and password to access your account';
}
}
if(isset($error)){ echo $error;}
?>
</body>
</html>

lindaonline15

3:38 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



what source? do you mean php.ini? and what should be there actually?
sry if I'm quite slow.. I dont have much knowledge and got confused..

I actually was thinking of executing query as well, I did change the query to $q and run it with mysql_query... but still the same damn blank page of login.php

to be much clearer, my login was working well before, all of this happened just when I started to add access level of users. and I really needed to add it...

lindaonline15

3:47 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



ok... I changed my login.php code to this:

<html>
<head>
<title>Login</title>
<head>

<body>

<?PHP

//Connect to database

include_once "connection.php";

session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);

$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";

if ($_POST[username]&&$_POST[password]) $result=mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = "Bad Login";
include "login.html";

} else {
$row = mysql_fetch_assoc($result);
$_SESSION['username'] = $row['username'];
$_SESSION['logged_in'] = TRUE;
print("<h3>Welcome $username!</h3>");
include "members.php";
}

?>

</body>
</html>

now I'm logging in, but with an error:

Welcome nanaz!

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\members.php on line 12

this is my members.php code:
<html><head><title>Feedbacks</title></head>

<body>

<?php
include_once "connection.php";

9 include "checklogin.php"
10session_start();
11checkLogin('0 1 2');

?>

<h3>Feedbacks</h3>
<table border = "1">
<tr><strong>
<td align = "center"><strong>feedback id</strong></td>
<td align = "center"><strong>Subject Code</strong></td>
<td align = "center"><strong>Semester</strong></td>
<td align = "center"><strong>Year</strong></td>
<td align = "center"><strong>Comment</strong></td>
<td align = "center"><strong>Date</strong></td>
</tr>

<?php

$query = "SELECT * FROM feedbacks";
$result = mysql_query($query) or die (mysql_error());
$num=mysql_num_rows($result);
?>

<?php

/*
$row = mysql_fetch_assoc( $result );
echo "<pre>";
var_dump($row);
echo "</pre>"; */

while ($row = mysql_fetch_assoc( $result ))
{
$fid = $row['feedback_id'];
$sc = $row['subject_code'];
$sem = $row['Semester'];
$y = $row['Year_of_exam'];
$dtls = $row['details'];
$dat = $row['date'];

print("<tr>");
print("<td>$fid</td>");
print("<td>$sc</td>");
print("<td>$sem</td>");
print("<td>$y</td>");
print("<td>$dtls</td>");
print("<td>$dat</td>");
print("</tr>");
}
?>

</table>

<br /><strong>
<?php print("$num") ?> feedback(s).</strong> <br/> <br />

<?php
mysql_close();
?>

</table>
</body>
</html>

as you can see, line 10 is session_start(). Im using this to apply the access level. anything wrong?

jatar_k

3:48 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you are missing a semi colon

include "checklogin.php"

wheelie34

3:49 pm on Sep 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a dumb question, are you sure the user is IN the db? if the user is, try deleting the user and re-adding if it's possible, I went round and round with a login a month ago and somehow the user details were correct but corrupted in some invisible way.

I take it you would like to redirect certain users to certain welcome pages, depending on their level of authorisation, I run something similar on a site of mine, I run it over 3 different pages though, it seemed easier at the time, login.php sends the details to who.php which checks the db for A) a valid user (num_rows) then B) the page to send them to. Heres the code of who.php


if (mysql_num_rows($result) == '1')
{
if ($usertype == 'user')
{
header("Location: http://www.example.com/user_welcome.php?id=$user&passwd=$passwd");
exit;
}
if ($usertype == 'admin')
{
header("Location: http://www.example.com/boss_welcome.php?id=$user&passwd=$passwd");
exit;
}
if ($usertype == 'subadmin')
{
header("Location: http://www.example.com/subadmin_welcome.php?id=$user&passwd=$passwd");
exit;
}
}
else
{
header("Location: http://www.example.com/login.php");
exit;
}

HTH a bit

lindaonline15

4:09 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



may I know how exactly to send data from login page to who page? I'm sick and tired of not getting respond..

lindaonline15

4:24 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



oh my god..
i just got it!
it was a SILLY STUPID mistake!
just a HTML mistake!
in checklogin.php
html started like this:
<html>
<head />
<title />
<body>
.
.
.
can you see the problem with head an title?
oh my god... I cant believe I was struggling for hours just because of stupid html mistake!
anyway, wheelie, thank you so much. you helped alot with patients.. and I learned alot in this struggle;)

good luck..