Access Levels

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Access Levels

Post by Epiales »

Okay, I always spend hours trying before I jump in here. Gets frustrating...

What's wrong here? I can't get my access levels working :( :oops:

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{

$query = "SELECT * FROM user WHERE username='$username'";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
          
            if ($row['access'] == 1);

header("Location: logout.php");

if ($row['access'] == 5) 
        
header("Location: index.php");
              
}

?>
I tried putting an else in there, but everything I do I get an error :(
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Access Levels

Post by Epiales »

Have also tried this:

Code: Select all

<?php
session_start();
include("functions.php");
connect();

$query = "SELECT access from user WHERE id = '$id'";
       $result = mysql_query($query) or die(mysql_error());
       if(!$result) die ("Database access failed: " .mysql_error());
          $row = mysql_fetch_array($result); 

            if (mysql_num_rows($result) == 5) {
        
header("Location: index.php");

} else {
header("Location: logout.php");
              
}

?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Access Levels

Post by MikuzA »

:)

mysql_num_rows in your case equals 1. due It's an function to retreive how many rows your query resulted.

Your first code looks more on the right track, I would adjust those if-sentences a bit.
You could do some debugging, like I bet you expect that the database returns a '5' in column access.

print it out to confirm you get this, then start adding the if's to validate what happens when access is something else.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Access Levels

Post by Epiales »

I'm not sure how to debug really :( :oops: :oops:

But I have this now. It really does nothing but say the page has a redirect and won't go anyway lol...

I know I'm close lol

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(isset($_SESSION['uid'])){


$query = "SELECT * FROM user WHERE id='$id'";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
          
            if ($row['access'] == 1)
{
header("Location: logout.php");

}else {  ($row['access'] == 5); 
        
header("Location: index.php");
              
}
}
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Access Levels

Post by MikuzA »

add a print $row['access']; or DIE($row['access']);
so that you can see does it have any value.
that's like trace-debugging.

Also, I would suggest doing the if-thingie reversed,

IF($access ==5) { admin awesomeness; }
elseif($access == 3) { gamemaster toolset; }
else { player access; }
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Access Levels

Post by Epiales »

Okay, I put this in there:

Code: Select all

<?php
session_start();
include("functions.php");
connect();
if(isset($_SESSION['uid'])){


$query = "SELECT * FROM user ";
       $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result); 
        print $row['access'];  
            if ($row['access'] == 1)

?>
And it returned the value of 5, which is admin
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Access Levels

Post by hallsofvallhalla »

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Code: Select all

if ($row['access'] == 1)
{
header("Location: logout.php");
exit;
}
if ($row['access'] == 5) 
{ 
header("Location: index.php");
exit;
}       
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Access Levels

Post by Epiales »

hallsofvallhalla wrote:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Code: Select all

if ($row['access'] == 1)
{
header("Location: logout.php");
exit;
}
if ($row['access'] == 5) 
{ 
header("Location: index.php");
exit;
}       
I swear I've had it that way before. I put it and I just get the page has a redirect and won't access it either way.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Access Levels

Post by hallsofvallhalla »

wait, what do you get on the page?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Access Levels

Post by hallsofvallhalla »

are you getting the "this page has a redirect loop"?

It is most likely because your index page has a redirect on it as well. If this code is going in the index page then do not put the header redirect of index. Instead do this

Code: Select all

if ($row['access'] < 5)
{
header("Location: logout.php");
exit;
}

/// rest of page will run only if access level is 5. 
   
Post Reply

Return to “Beginner Help and Support”