elexion Posted January 12, 2009 Share Posted January 12, 2009 hey guys thanks for helping me out on my last problem right now i'm strugeling with my navigation page i want it to show other links when role is admin. However it's not working so well i use a : if($SESSION['role']=='admin') statement in my navigation script to define the user that logged in. so now i'm trying to define that $SESSION variable from the page its been send from but i have no idea what to enter so far i have $_SESSION['role']=='';sorry for my probably very annoying question (i'm good at making a mess of my script) but i'm still learning.thanks in advance. Link to comment Share on other sites More sharing options...
Lulzim Posted January 12, 2009 Share Posted January 12, 2009 probably you know it, but just a reminder: you have to use session_start() at the top of every php script that you're going to use $_SESSION Link to comment Share on other sites More sharing options...
jeffman Posted January 12, 2009 Share Posted January 12, 2009 I'm not quite sure what the question is. Could you rephrase it without using the word 'define.' Link to comment Share on other sites More sharing options...
zppblood Posted January 12, 2009 Share Posted January 12, 2009 When you assign a value to a variable you use the assignment operator (=), and if you want to check for equality you use ==.$_SESSION is what you want to use, not $SESSION.So when the user submits the information for the username and password, and the username exists, check to see if that user is an admin. If they are assign the $_SESSION['role'] with admin, if not don't assign it admin. Then use the conditional to check the value of the $_SESSION['role']. Link to comment Share on other sites More sharing options...
jlhaslip Posted January 12, 2009 Share Posted January 12, 2009 A portion of a Login header script I use: // Display Admin Pages if( $_SESSION['role'] == "admin" ){ echo ' <li class="admin" ><a href="#" title="Admin Link">Admin link</a></li>'; } And I would be remiss if I failed to suggest that on those Admin Link pages, you would need to check their status again so that the page is not being linked to directly. What good would it do to have the menu withhold a link if the regular members can hotlink to it? Link to comment Share on other sites More sharing options...
elexion Posted November 18, 2009 Author Share Posted November 18, 2009 well guys hate to say this but i'm afraid i have to make this post active again, Had this problem fixed but due to the blue screen of death i lost my progress So i'm trying to make the code display who's logged in right now i have this: <?php session_start(); ?><html><ul><?phpinclude("connect.php");$username = $_SESSION['gebruikersnaam'];if($username != ''){ echo("docent username");}else{echo(" noooo i failed again ");}?></ul></html> Undefined index, so yeah not quite sure how to get the usersname to be displayed.the user who's logged in should be displayed.thanks for the help in advance. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 18, 2009 Share Posted November 18, 2009 If it says undefined index then that variable was never set in the session. If you want to see what's in the session you can use print_r:print_r($_SESSION); Link to comment Share on other sites More sharing options...
elexion Posted December 14, 2009 Author Share Posted December 14, 2009 when i use the print_r($_SESSION); line it tells me theres a Array() in my session, Does that mean i'm on the right track or that theres something wrong with my log in script? Link to comment Share on other sites More sharing options...
Synook Posted December 14, 2009 Share Posted December 14, 2009 $_SESSION is an array. If it shows just Array(), you don't have anything in it. Link to comment Share on other sites More sharing options...
elexion Posted December 14, 2009 Author Share Posted December 14, 2009 so that means my log in script is not working properly? <?phpsession_start ();?><!DOCTYPE htmlpublic "-//W3C//DTD XHTML 1.0 strict //EN""http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd"><html><head><title> logging please stand by </title></head><body><?phpinclude ("connect.php");$wachtwoord = ($_POST["wachtwoord"]);$query = "SELECT * from docentenWHERE gebruikersnaam = '{$_POST['gebruikersnaam']}'AND wachtwoord = '".$wachtwoord."'";$result = mysql_query($query, $db) or die('the query failed');if (mysql_num_rows($result) > 0){echo "correct";$rij = mysql_fetch_array($result);echo $rij['gebruikersnaam'];$_SESSION['gebruikersnaam']=='';header("location: index.php");}else{echo "usersname/password is incorrect";}?></body></html> that's what my log in script looks like now. Not sure if theres a problem here if i enter the proper date it does send me to index.php but the print_r($_SESSION); line keeps telling me i'm still with a empty array. Link to comment Share on other sites More sharing options...
Synook Posted December 14, 2009 Share Posted December 14, 2009 $_SESSION['gebruikersnaam']==''; Why are you using the equality operator? Link to comment Share on other sites More sharing options...
elexion Posted December 14, 2009 Author Share Posted December 14, 2009 $_SESSION['gebruikersnaam']==''; Why are you using the equality operator? a good question changing that line to:$_SESSION['gebruikersnaam']=''; seems to be giving me a lot better results now it'sdisplaying the message : Logged in as: Array([gebruikersnaam] =>'admin')which is good cause that's the usersname i'm using to log in at the moment justnot quite sure how to make it only display "logged in as: Admin"the code checks for who's logged in using the menu so the name will be displayed all over thewebsite i think i have a bug in here too cause it's not working properly <?phpinclude("connect.php");$username = $_SESSION['gebruikersnaam'];if($username != ''){ echo(" ingelogt als: "); echo $_SESSION;}else{}?> just for the record i do start a session at the top of the code.problem now is when i'm not logged in i get the undefined index messageand when i'm logged in i get "logged in as: Array" rather then Admin Link to comment Share on other sites More sharing options...
Err Posted December 14, 2009 Share Posted December 14, 2009 <?phpinclude("connect.php");$username = (isset($_SESSION['gebruikersnaam'])) ? $_SESSION['gebruikersnaam'] : "";if ($username){ echo(" ingelogt als: $username");}else{ echo " not logged in";}?> Corrected. Please note the changes. Also, when checking for any value inside a variable, ($variable) is the same as ($variable != ''). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.