Jump to content

Trying To Add / Hide Certain Menu Links Using Php


kjetterman

Recommended Posts

So, I am currently working on a site where my goal is to show a standard set of links to those who are not logged in. For those who are members and are logged in, I want to show additional links that are pertinent to any member logged in.

 

Here is my code:

 

<?php
$loggedout = "/index.php";
$loggedin = "/photog_main.php?pmode=usermain";
$currentpage = $_SERVER['REQUEST_URI'];

if ($currentpage == $loggedin)
 echo '<div class="two-thirds2">';
else {
 echo '<div class="two-thirds">';
}


?>

 

As you can probably guess, it isn't working. One of the issues I am having is that both div classes show at the same time regardless. So, one option (if I am thinking this through), would be to add additional lines -- so instead of echo '<div class="two-thirds2">'; I could probably do something like: echo '<li><a href="#">My link</a></li>'; .... ??

 

Thank you for any input / advice you may have on how to solve the issue. B)

Link to comment
Share on other sites

I'm really not following your code or your statement that "both div classes show at the same time regardless". That would be impossible based upon that if/else condition.

 

Since I assume the links are inside the divs you need to put the logic where you output the links - not the containing div(s).

 

//Set var $loggedIn to true/false

echo "div class='two-thirds2'>";

//Standard links for all users
echo "<li><a href="#">Link 1</a></li>\n"
echo "<li><a href="#">Link 2</a></li>\n"
echo "<li><a href="#">Link 3</a></li>\n"

//Links for logged in users
if($loggedIn)
{
   echo "<li><a href="#">Link 4</a></li>\n"
   echo "<li><a href="#">Link 5</a></li>\n"
   echo "<li><a href="#">Link 6</a></li>\n"
}

echo "</div>\n";

Link to comment
Share on other sites

Thank you for your help!

 

I'm sure the solution to this is probably more simple than I am making it out to be. I'm learning my way and I really appreciate your help. :)

 

Okay so here is the menu markup:

 

<div class="two-thirds">
       <h2 class="hfooter">Quick Links</h2><br />
       <li><a href="about_us.php">About Us</a></li>
       <li><a href="dioCon.php?conID=38">Services</a></li>
       <li><a href="adorder.php">Request Advertising Information</a></li>
       <li><a href="contact.php">Contact Us</a></li>
   </div>

 

I want to have the following items "show up" as additional links when a user logs in:

 

<li><a href="photog_main.php?pmode=sendfile">Send File</a></li>
    <li><a href="download_graphics.php">Download Center</a></li>
    <li><a href="download_covers.php">Download Special Covers</a></li>
    <li><a href="photog_main.php?pmode=profile">Your Profile</a></li>

 

My (obviously wrong) answer to that initially was to create two separate divs -- one with the short menu (two-thirds) and one with the full menu (two-thirds2).

 

So then, I took what I had and re-wrote it to match your logic:

 

<?php
   $loggedIn = '/photog_main.php?pmode=usermain';

   echo '<div class="two-thirds">';

//Standard links for all users
echo '<li><a href="#">Link 1</a></li>';
echo '<li><a href="#">Link 2</a></li>';
echo '<li><a href="#">Link 3</a></li>';

//Links for logged in users
if($loggedIn)
{
       echo '<li><a href="#">Link 4</a></li>';
    echo '<li><a href="#">Link 5</a></li>';
    echo '<li><a href="#">Link 6</a></li>';
}

echo '</div>';


   ?>

 

It isn't working -- but I think that's because something is missing on my end of things. *scratches head*

Link to comment
Share on other sites

cleaned up your code using HEREDOC syntax instead of abuch of echos, can you please explain how your login currently works? like if it checks for login or logout sessions or even checks the database.

 

 

<?php
 $loggedIn = '/photog_main.php?pmode=usermain';
<<<EOL
<div class="two-thirds">
EOL;

//Standard links for all users

<<<EOL
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>

EOL;

//Links for logged in users
if($loggedIn)
{
<<<EOL
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
EOL;
}

<<<EOL
</div>
EOL;
?>

Edited by darkfreaks
Link to comment
Share on other sites

gah took to much time but something like this should work

 

 

<?php

$loggedout = "/index.php";
$loggedin = "/photog_main.php?pmode=usermain";
$currentpage = $_SERVER['REQUEST_URI'];





<<<EOL
<div class="two-thirds">
EOL;

//Standard links for all users

<<<EOL
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>

EOL;

//Links for logged in users
if($currentpage== $loggedin)
{
<<<EOL
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
EOL;
}

<<<EOL
</div>
EOL;
?>

Edited by darkfreaks
Link to comment
Share on other sites

also noticed that was the exact same code you used LOL.

 

 

did some research and made some changes *crosses fingers*

 


<?php

$loggedin = "/photog_main.php?pmode'=".$_GET['usermain']."'";

//** uncomment if script designed to grab URL instead of script path.**//
//** $currentpage = explode('?',basename($_SERVER['REQUEST_URI'])); **//
//** should output something like index.php & mysite.com/index.php?foo=something **//
$currentpage = explode('?',basename($_SERVER['SCRIPT_NAME']));





<<<EOL
<div class="two-thirds">
EOL;

//Standard links for all users
if($currentpage=='index.php')
{
<<<EOL
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>

EOL;
}
//Links for logged in users
elseif($currentpage== $loggedin)
{
<<<EOL
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
EOL;
}

<<<EOL
</div>
EOL;
?>

Link to comment
Share on other sites

@darfreaks,

 

Not sure what you are doing, except complicating the problem. There was nothing wrong with the original logic (except one thing). trying to introduce things such as heredoc syntax to someone who is not familiar is only going to complicate the issue.

 

@kjetterman,

 

The only problem I see with the code you posted in response to my post is that you are not defining $loggedIn correctly. You simply hard coded it to be a partial URL path. I noticed that in your first post you were using some logic such as

if ($currentpage == $loggedin) 

 

Where $currentpage and $loggedin were the Request URI and a hard coded value. I have no idea why that would be the determining factor as to whether a user is logged in or not. Which is why I put in my code:

//Set var $loggedIn to true/false 

 

I guess I should have been more explicit. You need to have a process when the user logs in to set a value. This is typically done using a session value. You would then check that session value on each page load to determine if the user is logged in or not. Using the URI makes no sense.

Link to comment
Share on other sites

So, I am currently working on a site where my goal is to show a standard set of links to those who are not logged in. For those who are members and are logged in, I want to show additional links that are pertinent to any member logged in.

 

Here is my code:

 

<?php
$loggedout = "/index.php";
$loggedin = "/photog_main.php?pmode=usermain";
$currentpage = $_SERVER['REQUEST_URI'];

if ($currentpage == $loggedin)
echo '<div class="two-thirds2">';
else {
echo '<div class="two-thirds">';
}


?>

 

As you can probably guess, it isn't working. One of the issues I am having is that both div classes show at the same time regardless. So, one option (if I am thinking this through), would be to add additional lines -- so instead of echo '<div class="two-thirds2">'; I could probably do something like: echo '<li><a href="#">My link</a></li>'; .... ??

 

Thank you for any input / advice you may have on how to solve the issue. B)

 

 

Looking at your code, there seems to be no actual credential in place to check if a user is actually "logged in", but rather just what url they are on. By this logic, I do not need to login anyway, but just visit a certain page of your website? This needs to be recoded.

 

Before you look into seperate menus for users based on their url, you should first look at seperate menus for users based on a login credential, such as a cookie or PHP session.

 

If you use cookies, my browser will be updated with information telling your script I am either a logged in, or out. Rather than checking the URL to see this fact, we check my cookie. If no cookie exists - I am logged out. If a cookie exists but has expired - I am logged out. If a cookie exists and is active - I am logged in.

 

Then, based on the status of my cookie, you can show the appropriate menus.

 

You also need to secure the validation of your cookie and make sure it refences something (such as correct login credential in a database), otherwise I could just set a cookie in my browser to access your site.

 

I have probably over-explained it and gone into too much detail security wise for a newbie, but it is good to plug these mistakes early on.

 

My best suggestion would be looking into PHP sessions. Check out this free resource: http://www.phpeasystep.com/phptu/6.html and from here, you can probably see the if statements which determine a users status. From there, you can show menus depending on that.

Link to comment
Share on other sites

Thank you! Thank you! You guys don't know how much I appreciate this! I am very eager to learn!

 

 

Here is what I came up with last night and it actually WORKS!!!

 

<?php
session_start();

if ('logout' == $action) {
   session_destroy();
}


   echo '<div class="two-thirds">';
   echo '<h2 class="hfooter">';
   echo 'Quick Links';
   echo '</h2>';
   echo '<br />';

//Standard links for all users
echo '<li><a href="#">Link 1</a></li>';
echo '<li><a href="#">Link 2</a></li>';
echo '<li><a href="#">Link 3</a></li>';

//Links for logged in users
$loggedIn = isSet($_SESSION['/photog_main.php?pmode=usermain']);

if($_SESSION['photog_id']){

       echo '<li><a href="#">Link 4</a></li>';
    echo '<li><a href="#">Link 5</a></li>';
    echo '<li><a href="#">Link 6</a></li>';
}

echo '</div>';    

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.