1

Please, I'm new to PHP and I'm building a subscription-based ecommerce site. I've been able to customize the pages such that they look different based on who's browsing what, but I'd like to take the Renew Account off the admin page since they don't have to subscribe.

I've been struggling all day. Please, could someone show me how it could be achieved? Below is the snippet:

<?php // Show the user info or the login form:
 if (isset($_SESSION['user_id'])) {

    // Show basic user options:
    echo '<div class="title">
                <h4>Manage Your Account</h4>
            </div>
            <ul>
            <li><a href="renew.php" title="Renew Your Account">Renew Account</a></li>
            <li><a href="change_password.php" title="Change Your Password">Change Password</a></li>
            <li><a href="favorites.php" title="View Your Favorite Pages">Favorites</a></li>
            <li><a href="history.php" title="View Your History">History</a></li>
            <li><a href="recommendations.php" title="View Your Recommendations">Recommendations</a></li>
            <li><a href="logout.php" title="Logout">Logout</a></li>
            </ul>
            ';

    // Show admin options, if appropriate:
    if (isset($_SESSION['user_admin'])) {
        echo '<div class="title">
                    <h4>Administration</h4>
                </div>
                <ul>
                <li><a href="add_page.php" title="Add a Page">Add Page</a></li>
                <li><a href="add_pdf.php" title="Add a PDF">Add PDF</a></li>
                <li><a href="#" title="Blah">Blah</a></li>
                </ul>
                ';      
    }

} else { // Show the login form:

    require ('login_form.inc.php');

}
?>
11
  • What is the question? I don't understand what you're asking. What isn't working the way you would like it to? Commented Nov 15, 2018 at 21:24
  • I don't want the Renew Account to show under the admin side Commented Nov 15, 2018 at 21:26
  • See the answer given below. Commented Nov 15, 2018 at 21:26
  • What is the status of the question? Given the answer; does it work or not? Commented Nov 15, 2018 at 21:37
  • It doesn't work. Could there be another way? Commented Nov 15, 2018 at 21:44

2 Answers 2

0

What you could do is to first check if the user admin session is set, to which I added the same conditional value for in the first conditional statement.

If it is set, then assign an empty value for what I named as $renew, with an else{} with the value that I removed from your existing <li></li>.

The first two session arrays here are only representational values of course.

I concatenated the '.$renew.' variable in the menu.

Note: Make sure that the session was started using session_start() inside all pages using sessions; that is not known.

$_SESSION['user_id'] = 123;
$_SESSION['user_admin'] = "john";

if (isset($_SESSION['user_admin'])) {

$renew = '';

} else {

$renew = '<li><a href="renew.php" title="Renew Your Account">Renew Account</a></li>';

}


if (isset($_SESSION['user_id'])) {

    // Show basic user options:
    echo '<div class="title">
                <h4>Manage Your Account</h4>
            </div>
            <ul>
            '.$renew.'
            <li><a href="change_password.php" title="Change Your Password">Change Password</a></li>
            <li><a href="favorites.php" title="View Your Favorite Pages">Favorites</a></li>
            <li><a href="history.php" title="View Your History">History</a></li>
            <li><a href="recommendations.php" title="View Your Recommendations">Recommendations</a></li>
            <li><a href="logout.php" title="Logout">Logout</a></li>
            </ul>
            ';

    // Show admin options, if appropriate:
    if (isset($_SESSION['user_admin'])) {
        echo '<div class="title">
                    <h4>Administration</h4>
                </div>
                <ul>
                <li><a href="add_page.php" title="Add a Page">Add Page</a></li>
                <li><a href="add_pdf.php" title="Add a PDF">Add PDF</a></li>
                <li><a href="#" title="Blah">Blah</a></li>
                </ul>
                ';      
    }

}
5
  • It seems not to work. I The Renew Account doesn't show, but I keep seeing the same log admin content as though I haven't logged out. I don't know what could possibly be the cause. PS I have session_start () in all pages that require session Commented Nov 15, 2018 at 22:43
  • @jessebrite What I posted worked for me. Make sure that caching isn't an issue here and/or that the previous session doesn't still exist and needs to be destroyed and a logout method does use the function to destroy it. Commented Nov 15, 2018 at 22:46
  • @FunkyFortNiner I finally got it to work! I removed the two session variables as seen in your code since they'll be coming from other pages. Your code is a life saver. Thanks@ColbyBoren as well. You guys are awesome Commented Nov 15, 2018 at 22:49
  • @FunkyFortyNiner How's it done? Is it different from upvoting? Commented Nov 15, 2018 at 22:58
  • @jessebrite There's a gray'ish checkmark just under the up/down arrows that you tick. When it turns green, it is marked as accepted and the question is then marked as solved. Commented Nov 15, 2018 at 22:59
0

Try..

If(!isset($_SESSION['user_admin'])) {echo '<li><a href="renew.php" title="Renew Your Account">Renew Account</a></li>'; }

This way if it the session variable for admin is set the echo won't occur.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.