php - Showing different content based on $_SESSION UserLevel -
i having problem trying show different menu options based on userlevel. have mysql database users table. users table contains userlevel either set 0 or 1. reason php isn't working. in fact, when add php menu, not display on site below menu. advice appreciated.
code starts session
<?php if(!empty($_session['loggedin']) && !empty($_session['username'])) { ?> <?php include "mainnav.php"; ?> <center> <h2> campaign updates</h2> </center> <div id="campaignpostwrap"> <div id="campaignscrollbox"> <?php $con=mysqli_connect("localhost","dorians","ds2953!b67p$","aldentec"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $result = mysqli_query($con,"select * campaigns order postdate desc"); while($row = mysqli_fetch_array($result)) { echo "<div id='campaignpostcontainer'>"; echo "<ul class='campaignpostbox'>"; echo "<p class='postinfo'>"; echo "posted on:"; echo "<li>" . $row['postdate'] . "</li>"; echo "</p>"; echo "<p class='postinfo'>"; echo "posted by:"; echo "<li>" . $row['postname'] . "</li>"; echo "</p>"; echo "<li class='postentry'>" . $row['postentry'] . "</li>"; echo "</ul>"; echo "</div>"; echo "<hr>"; } mysqli_close($con); ?> </div> <?php include "campaignpost.php"; ?> </div> <?php include "chat.php"; ?> <?php } elseif(!empty($_post['username']) && !empty($_post['password'])) { $username = mysql_real_escape_string($_post['username']); $password = md5(mysql_real_escape_string($_post['password'])); $checklogin = mysql_query("select * users username = '".$username."' , password = '".$password."'"); if(mysql_num_rows($checklogin) == 1) { $row = mysql_fetch_array($checklogin); $email = $row['emailaddress']; $userlevel = $row['userlevel']; $_session['username'] = $username; $_session['emailaddress'] = $email; $_session['loggedin'] = 1; $_session['userlevel'] = $userlevel; echo "<h1>success</h1>"; echo "<p>we redirecting member area. if not automatically redirected <a href='index.php'>click here</a></p>"; header( "refresh:10;url=index.php" ); } else { echo "<h1>error</h1>"; echo "<p>sorry, account not found. please <a href=\"index.php\">click here try again</a>.</p>"; } } else { ?>
menu code isn't working
<?php session_start(); ?> <?php $userlevel = $_session['userlevel']; if($userlevel == 0) { echo "<ul class="mainnav"> <a href="characterroll.php"><li> create character</li></a> <a href="questcreate.php"><li> create quest</li></a> <a href="characterview.php"><li> view characters</li></a> <a href="questlist.php"><li> view quests</li></a> <a href="booksview.php"><li> book list</li></a> </ul>"; } elseif($userlevel == 1) { echo "<a href="dmtools.php"><li> dm tools</li></a>"; } ?> <?php include "greeter.php"; ?>
your quotes undoubtedly problem here:
if($userlevel == 0) { echo "<ul class="mainnav"> <a href="characterroll.php"><li> create character</li></a> <a href="questcreate.php"><li> create quest</li></a> <a href="characterview.php"><li> view characters</li></a> <a href="questlist.php"><li> view quests</li></a> <a href="booksview.php"><li> book list</li></a> </ul>"; } elseif($userlevel == 1) { echo "<a href="dmtools.php"><li> dm tools</li></a>"; }
notice syntax highlighting above shows issue in string. see how turns black when gets mainnav
? that's because mainnav
no longer part of string. that's bad thing here.
look @ first line of echo
:
echo "<ul class="mainnav">
you open quote , close @ class="
. now, it's trying evaluate mainnav
constant or other language construct. on top of that, doesn't know mainnav
haven't provided kind of operators.
instead, should like:
if($userlevel == 0) { echo '<ul class="mainnav"> <a href="characterroll.php"><li> create character</li></a> <a href="questcreate.php"><li> create quest</li></a> <a href="characterview.php"><li> view characters</li></a> <a href="questlist.php"><li> view quests</li></a> <a href="booksview.php"><li> book list</li></a> </ul>'; } elseif($userlevel == 1) { echo '<a href="dmtools.php"><li> dm tools</li></a>'; }
alternatively, escape every location there non-string-terminating quote \"
.
another option use heredoc syntax.
Comments
Post a Comment