gibbo1715 Posted December 1, 2009 Share Posted December 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/ Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968907 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968920 Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968935 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968945 Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 Sorry, this line: for($x = 0; $x < count($_SESSION['permissions']); $x++) { Be aware that the code I have posted is not a fully working model. You would have to integrate it into your design yourself. Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968946 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 Thanks I tried that change but still get the same error I appreciate your help and happy to link this into my own environment when i ve got my head around it but i assume the above shld now work? thanks again gibbo Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968952 Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 for($x = 0; $x < count($_SESSION['permissions']); $x++) { This is definately correct. Look for any missing ; as your error states. I assume you are familiar with syntax errors Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968957 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968964 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 Cancel that, forgot the session_start(); variable at the satart of my index2 page helps to read up a bit thanks again for all your help gibbo Quote Link to comment https://forums.phpfreaks.com/topic/183562-config-to-set-what-is-visible-to-my-users/#findComment-968974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.