SO i WAS TESTING my code..and it doesnt seem to work...here it is

<?php
require_once('../includes/config.php');
if ($_Session['roleID']=1) {
                    echo "admin";
                }           
                elseif ($_Session['roleID']=2) {
                    echo "faculty";
                }


?>

Recommended Answers

All 35 Replies

Try starting the session at the top
<?php session_start(); ?>

And $_SESSION should be in uppercase

The variable session must be always uppercase, so $_Session is not correct, change it to $_SESSION:

$_SESSION['roleID']

The session_start(); is included in the config file?

Also, in the IF statement you have to compare, not assign a value, so this:

if ($_Session['roleID']=1)

Becomes:

if ($_Session['roleID'] == 1)

When you're testing booleans, instead, use === as explained here:

Apply the same to the ELSEIF statement.

Oh so it sould be like == not =.....i see thanks but i get an error dude..it says

Parse error: syntax error, unexpected 'if' (T_IF) in....

Are you getting an error even after applying the changes that cereal mentioned? or Does $_SESSION['roleID'] contain a value other than 1 or 2?

the main point here is that I retrieve the roleid of the currently logged in user....its just a testing code so it doesnt seem that way..did I use the right method to retrieve roleID from my database?$_Session['roleid']?..

did I use the right method to retrieve roleID from my database?

Show the code you have for that.

I made a mistake in my previous post, this:

if ($_Session['roleID'] == 1)

Should be:

if ($_SESSION['roleID'] == 1)

Uppercase! About this: Parse error: syntax error, unexpected 'if' (T_IF) in... read the full error code. It can happen because of a missing ;, for example:

<?php

echo 'hello'

if(1 == 1) echo ' world';

Will return: Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in .... So check if the code in the config file is correct.

If you want to check the session status just try this in a test page:

<?php

    session_start();

    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";

It will return all the values saved in the current session.

forgot a semicolon but now it shows.." Notice: Undefined index: roleID in C:\wamp\www\simpleblog\admin\menu.php on line 5 and 8...

unfortunately this gives an error..i just want to store the roleid of the current logged in user to $roleid so my if statement would work...Parse error: syntax error, unexpected 'roleID' (T_STRING) in C:\wamp\www\simpleblog\admin\menu.php on line 4

<?php

include_once('../includes/config.php');
$roleID=('SELECT roleID from blog_members where roleID=$_SESSION['roleID']');

if ($roleID == 1) {
                    echo "admin";
                }           
                elseif ($roleID == 2 ) {
                    echo "faculty";
                }


?>

Line 4 uses a single quoted string., You cannot put a variable in it, use a double quoted string:

$roleID = ("SELECT roleID from blog_members where roleID={$_SESSION['roleID']}");

Next problem is that $roleID is just a string, you need to execute the query.

thanks but what the hell is this? i cant fix it.
Notice: Undefined index: roleID in C:\wamp\www\simpleblog\admin\menu.php on line 4...

Notice: Undefined index: roleID

That means that the $_SESSION['roleID'] never got a value.

thats impossible roleid in blog_members table has a value..currently i have 2 users...one having a roleid of 1 and the other having a roleid of 2.....hmmmm...

Pritaeas-sensei, please help...note: im an effin beginner..just started playing with php last week

First try Disabling the error reporting first, by adding;

error_reporting(0);

at the top of your page.
Let us know whether it works or not.

lol..it doesnt work...nothing is retrieved from the database then?omg...i dont know what to do

so i guess $_SESSION['roleid'] is not the right way:(

thats impossible roleid in blog_members table has a value

That it exists in your table, does not automatically mean it exists in your session. Do you have code to retrieve the role from the table? If so, show that piece.

oh..no i dont have that code..can you teach me how to?

I tried to look at some tutorials...let me know if im getting close sensei
unfortunately it still wont work

<?php
error_reporting(0);
include_once('../includes/config.php');
if(isset($_SESSION['roleID'])) 
{ 
    $result = mysql_query("SELECT roleID from blog_members where roleID='$_SESSION[roleID]'");
    $row = mysql_fetch_array($result); 
    if($row==1) 
    {
         echo "admin";
         }
  elseif ($row == 2 ) {
                    echo "faculty";
                }

}

?>

You didn't even connect to the database. I recommend you take the MySQLi or PDO example, NOT the deprecated MySQL one...

doesnt my config.php the one to handle connecting to database? here it is...:p sorry, i really am a novice..

<?php
ob_start();
session_start();
include('functions.php');

//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','uslt');

$db = new PDO("mysql:host=localhost; dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//set timezone
date_default_timezone_set('Europe/London');

//load classes as needed
function __autoload($class) {

   $class = strtolower($class);

    //if call from within assets adjust the path
   $classpath = 'classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }   

    //if call from within admin adjust the path
   $classpath = '../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }

    //if call from within admin adjust the path
   $classpath = '../../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }       

}

$user = new User($db); 

?>

doesnt my config.php the one to handle connecting to database?

Yes it does, but I couldn't see that.

The problem is that your config.php instantiates a PDO connection, so using the mysql_xxx() is not compatible with that. You need to be consistent.

<?php
error_reporting(0);
include_once('../includes/config.php');
// create a query that should return a single record
    // the backticks around the table and column names are optional
    // they are required only when a name matches a reserved word (e.g. `date`)
    $query = 'SELECT roleID FROM blog_members';
    // execute the query
    // query returns FALSE on error, and a result object on success
    try {
        $pdoStatement = $pdo->query($query);
    }
    catch (PDOException $exception) {
        // the query failed and debugging is enabled
        echo "<p>There was an error in query: $query</p>";
        echo $exception->getMessage();
        $pdoStatement = false;
    }
    if ($pdoStatement) {
        // the query was successful
        // get the result (if any)
        // fetchObject returns FALSE if there is no record
        if ($recordObj = $pdoStatement->fetchObject()) {
            // we have a record so now we can use it
            // the columns are properties of the object
            echo $recordObj->mycolumn;
        }
        else {
            echo '<p>No record found.</p>';
        }
        // when you are done with the statement, close it
        $pdoStatement->closeCursor();
    }

As I said Im a beginner and I dont fully understand it...I just replaced the query...

If you replace

echo $recordObj->mycolumn;

with

echo $recordObj->roleID;

do you see a value?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.