Register, Login and Logout button using PHP

8 replies
Hello members!
I have successfully created a website with a Registration and Login page. But still there is an issue. When someone logs in, the Login and Register button still exists in the menu. But as all of you know that, when someone logs in, the Login and Register buttons should disappear, and there should be a Logout button instead. Please help me, how can I write PHP code which can solve my problem.
#button #login #logout #php #register
  • Profile picture of the author KirkMcD
    Check if they are logged in and then display only the buttons you want?
    Where are you lost?
    {{ DiscussionBoard.errors[6909456].message }}
  • Profile picture of the author yoursswathi
    Basically you will create a session object when a user logins. If a session is created, then do not display the login button.
    {{ DiscussionBoard.errors[6911878].message }}
  • Profile picture of the author Big Squid
    You need to create a session when the user logs in as in...
    <?php
    session_start();
    if(!isset($_SESSION['user_id'])) {
    if(isset($_COOKIE['user_id'])) {
    $_SESSION['user_id'] = $_COOKIE['user_id'];
    }
    }
    ?>
    This code above will check to see if a session exists for the user. A session lasts until the browser is closed out. So it will also check to see if a cookie was set as well (google 'setcookie php' for more details.

    Now, anytime we want to check if a user is logged in, we simply call the $_SESSION['user_id'] variable.

    Now we need to apply that to your menu. So basically, we'll check the variable before writing the login/logout menu.
    <?php
    session_start(); // this will keep the user's session alive
    echo '<ul id="login_menu">';
    if(isset($_SESSION['user_id'])) {
    echo '<li><a href="logout.php">LOGOUT</a></li>';
    } else {
    echo '<li><a href="login.php">LOGIN</a></li>';
    echo '<li><a href="register.php">REGISTER</a></li>';
    }
    echo '</ul>';
    ?>
    You can make adjustments to the structure as you like, but the if statement is the key. If the user is logged in (isset) then you to display the appropriate menu. Otherwise, the other menu is displayed.

    If you need more help, please let me know...
    {{ DiscussionBoard.errors[6917514].message }}
  • Profile picture of the author teguhyuliantos
    You can modify and try to change your PHP script. But for the simple way and get perfect result, you can install paid widget plugin that you can found through internet. You can choose the kind of widget that you wish.
    {{ DiscussionBoard.errors[6918892].message }}
  • Profile picture of the author jmartinez
    You need to write code that verifies whether or not someoene is logged in on each page load. If they are, then load the logged in version of the page. If not, then bump them back to the login page. Then just update your logged in page template to include a logout button instead of login.
    {{ DiscussionBoard.errors[6922495].message }}
  • Profile picture of the author Nikolas
    Maybe you should use a solution like PHP User Class - php user class

    Otherwise if you want to fix what you have, you will need to play with $_SESSION and probably a database in order to keep track of your users.

    Hope that helps
    {{ DiscussionBoard.errors[6923764].message }}
  • try this...

    <form action="/login" method="post">
    <p>
    <label for="email">Email address:</label>
    <input id="email" type="text" name="email" />
    </p>
    <p>
    <label for="password">Password:</label>
    <input id="password" type="password" name="password" />
    </p>
    <p>
    <input type="submit" name="login" value="Login" />
    </p>
    </form>
    Signature
    WebDevelopmentGroup NYC & CA- Small Business Web Development, App Development, WordPress Development, Graphic Designs, Online Marketing, Local Marketing & more!. "Call us 1.800.219.1314 or message us!". Visit us today! "Now On Live Chat Mon-Fri.". www.WebDevelopmentGroup.org
    (Whitelable our Services)
    ===================================
    ==> #1 OFFLINE MARKETING FORUM ON THE WEB! <==
    www.OFFLINEMARKETINGFORUM.com
    (Register Now)
    {{ DiscussionBoard.errors[6924209].message }}

Trending Topics