Jump to content

config to set what is visible to my users


gibbo1715

Recommended Posts

Hi All

 

Im starting to get my head around php and i ve decided this is a really good language to program in but i have the following question please

 

I have figured out how to set up config files etc and call them from an includes.inc as follows

 

<?PHP

// INPUT/OUTPUT - PARAMETER
//---------------------------

// path to config files /extras/config_"config file name".inc

 	$config = "general"; //config file
    $configfile = "extras/config_$config.inc";  //path to config file

// Path to header directory

	$header = "head.inc.php";

// Path to footer directory

	$footer = "foot.inc.php";

//config_style.inc which gives extra config oportunity

	$style = "extras/config_style.inc";

//CSS file to use

	$css = "extras/css/color-chatstyle.css";


//************************************************


?>

 

Now i d like to set things up so i can have a load of perameters for my users in my sql data base and was wondering the best way to do this

 

i.e. i can have a users sql table containing the following

 

link1 = 0

link2 = 1

link3 = 0

link4 = 1

link5 = 1

link6 = 0

 

What is the best way of only showing my users what they are entitled to see based on this table, is it through a session or query the database each time.

 

If anyone has an exampe they could post that i could go through and learn from id really appreciate it

 

thanks

 

gibbo

I guess the user has to login, correct?

You could load the permissions into a session variable once the user login details have been validated (the code is purely hypothetical). It may also be better to strore the permissions in a single field separated by commas i.e 0,1,1,0

<?php
// login.php
session_start();
// validate user
if($validated == true) {
  // user validated - load in permissions
  $result = mysql_query("SELECT permissions FROM users WHER userId='x'");
  $row = mysql_fetch_assoc($result);
  $_SESSION['permissions'] = explode(",",$row['permissions']);
  // redirect user
  header("Location:index.php");
  exit();
}
else {
  // bad login
}
?>

Thats great thanks, two questions please

 

1. For each page I call from this point on do i need to call this code (Not read up too much on sessions yet)

 

2. I have a number of menu items and I only want to display them if access is permitted

 

so if i have menus

 

1

2

3

4

5

 

how do i get it to show

 

2

5

 

if my permissions are 0,1,0,0,1

 

thanks again

 

Gibbo

1. For each page I call from this point on do i need to call this code (Not read up too much on sessions yet)

No as the data is saved into a session upon login. It will exist until it is destroyed.

2. I have a number of menu items and I only want to display them if access is permitted

The permissions are exploded to an array so they will look like

0 => 0

1 => 1

2 => 0

3 => 0

4 => 1

So you can use a loop

<?php
$menus = array('menu1','menu2','menu3','menu4','menu5');
for($x = 0; $x < count($_SESSION['permissions']) $x++) {
  if($_SESSION['permissions'][$x]) {
// display the menu item
print $menus[$x]."<br />";
  }
}
?>

Do some reading on sessions. They will be needed to allow users to login to your website

Thanks

 

I ve tried putting this together as follows but get the error

 

Parse error: syntax error, unexpected T_VARIABLE, expecting ';' in C:\xampp\xampp\htdocs\b\My Template\index2.php on line 3

 

 

permissions.php

 

<?php
// login.php
session_start();
// validate user
//if($validated == true) {
  // user validated - load in permissions
  


$hostname='localhost'; //// specify host, i.e. 'localhost'
$user='root'; //// specify username
$pass=''; //// specify password
$dbase='shareddb'; //// specify database name
$connection = mysql_connect("$hostname" , "$user" , "$pass") 
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
  
  
  
  $result = mysql_query("SELECT permissions FROM table_name WHERE name='Paul'");
  $row = mysql_fetch_assoc($result);
  $_SESSION['permissions'] = explode(",",$row['permissions']);
  // redirect user
  
  header("Location:index2.php");
  exit();
//}
//else {
  // bad login
//}
?>

 

 

 

index2.php

 

<?php
$menus = array('menu1','menu2','menu3','menu4','menu5');
for($x = 0; $x < count($_SESSION['permissions']) $x++) {
  if($_SESSION['permissions'][$x]) {
// display the menu item
print $menus[$x]."<br />";
  }
}
?>


I suspect im not calling something properly but not sure what, can you explain what im doing wrong please

thanks

Gibbo

 

 

i should add that table_name is the name of the table  :) 

I can get this working fine on one page as follows 

 

<?php
// login.php
session_start();
// validate user
//if($validated == true) {
  // user validated - load in permissions
  


$hostname='localhost'; //// specify host, i.e. 'localhost'
$user='root'; //// specify username
$pass=''; //// specify password
$dbase='shareddb'; //// specify database name
$connection = mysql_connect("$hostname" , "$user" , "$pass") 
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
  
  
  
  $result = mysql_query("SELECT permissions FROM table_name WHERE ID='0'");
  $row = mysql_fetch_assoc($result);
  $_SESSION['permissions'] = explode(",",$row['permissions']);
  //$_session['test'] = $row['permissions'];
  // redirect user
  
  //header("Location:index2.php");
  //exit();
//}
//else {
  // bad login
//}

$menus = array('menu1','menu2','menu3','menu4','menu5');
for($x = 0; $x < count($_SESSION['permissions']); $x++) {
  if($_SESSION['permissions'][$x]) {
// display the menu item
print $menus[$x]."<br />";
  }
}
?>

 

 

but if i try in on two seperate pages as follows thats when i get the error

 

index2.php

<?php
$menus = array('menu1','menu2','menu3','menu4','menu5');
for($x = 0; $x < count($_SESSION['permissions']); $x++) {
  if($_SESSION['permissions'][$x]) {
// display the menu item
print $menus[$x]."<br />";
  }
}
?>

 

permissions.php

<?php
// login.php
session_start();
// validate user
//if($validated == true) {
  // user validated - load in permissions
  


$hostname='localhost'; //// specify host, i.e. 'localhost'
$user='root'; //// specify username
$pass=''; //// specify password
$dbase='shareddb'; //// specify database name
$connection = mysql_connect("$hostname" , "$user" , "$pass") 
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
  
  
  
  $result = mysql_query("SELECT permissions FROM table_name WHERE ID='0'");
  $row = mysql_fetch_assoc($result);
  $_SESSION['permissions'] = explode(",",$row['permissions']);
  // redirect user
  
  header("Location:index2.php");
  exit();
?>

 

 

im guessing im not passing things from one page to the other properly?

 

thanks

 

gibbo

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.