SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic log in / log out links

    This is hopefully an easy one - just trying to have a link that's to log in if no-one is logged in, and log out if they are.

    From a bit of a search, I realise it should be :

    Code:
    <?php
    if(isset($_SESSION['username'])) {
        echo "Logout";
    } else {
        echo "Login";
    }
    ?>
    Can anyone help me out with the syntax / code to define "Logout" and "Login"

    Cheers

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should be fine without isset as well. But this may vary from your codes. Make sure the session variable matches your current setting.
    My Network
    Deluxe Web Directory
    FontCubes Free Fonts

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Grumps View Post
    It should be fine without isset as well. But this may vary from your codes. Make sure the session variable matches your current setting.
    I'm getting confused with defining the variables for 'Log In' and 'Log Out' - where do I put the actual links?

    I've been trying with this sort of thing, but with no joy :

    Code:
    <?php
    $login="<a href="login.php">Login</a>";
    $logout="<a href="logout.php">Logout</a>";
    ?>

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    session_start();
    $login='<a href="login.php">Login</a>';
    $logout='<a href="logout.php">Logout</a>';
    if(isset(
    $_SESSION['username'])) {
        echo 
    $logout;
    } else {
        echo 
    $login;

    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know where I'm going wrong with this - if I have :

    Code:
    <?php
    if(($_SESSION["MM_username"]))
    {
    echo '<a href="logout.php>">Log Out</a>';
    } else {
    echo '<a href="login.php">Log In</a>';
    }
    ?>
    Shouldn't that be it?

    The session variable seems OK, as it outputs OK with a

    <?php echo $_SESSION['MM_Username'] ?>

    I'm getting the Log In link OK, but it's not switching to Log Out when I log in.

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where have you started your session ? Where do you load your user into your session variable ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to set $_SESSION['MM_username'] when they log in so the if statement becomes true.

  8. #8
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <pre>
    <?php
    if(isset($_SESSION['MM_username']))
    {
    echo 
    '<a href="logout.php>">Log Out</a>';
    } else {
    echo 
    '<a href="login.php">Log In</a>';
    }
    //check the session with
    print_r($_SESSION);
    ?>
    what si the otput?
    my mobile portal
    ghiris.ro

  9. #9
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The session starts on the login page, using Dreamweavers log in behaviour - all seems to work fine - just trying to incorporate this if.. then.. to determine the appropriate link.

    At the moment with a little test page, I have the log in link, takes me to the login page. If I log in and go back to the test page, the username is echoed correctly, but still with the log in link.

    And when I log out, the username isn't echoed (correct also), and with the log in linl.

    Anyway - should have gone an hour ago, so will look at it with a fresh head in the morning.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    PHP Code:
    <pre>
    <?php
    if(isset($_SESSION['MM_username']))
    {
    echo 
    '<a href="logout.php>">Log Out</a>';
    } else {
    echo 
    '<a href="login.php">Log In</a>';
    }
    //check the session with
    print_r($_SESSION);
    ?>
    what si the otput?
    Just recreated a few temp pages at home on my own site :

    Login page :

    http://www.handprintdvd.co.uk/login.php

    A menu temp page :

    http://www.handprintdvd.co.uk/menutemp.php

    With the code :

    Code:
     <?php
    if(($_SESSION["MM_username"]))
    {
    echo '<a href="logout.php>">Log Out</a>';
    } else {
    echo '<a href="login.php">Log In</a>';
    }
    //check the session with 
    print_r($_SESSION); 
    ?>
    
      <br>
      <br>
    
      <?php echo $_SESSION['MM_Username'] ?> is logged in.
    And a session start at the top.

    When no-one is logged in, the output is :

    Array ()

    When someone is logged in (you can try it with Iain and 'alaska'), it outputs :

    Array ( [MM_Username] => Iain [MM_UserGroup] => )

    The login doesn't change to logout tho'.

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Location
    Bel Air, Maryland.
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    session_start
    ();

    if (isset(
    $_SESSION['MM_Username']))
      echo 
    '<a href="logout.php>">Log Out</a>'
    else
      echo 
    '<a href="login.php">Log In</a>'
    try that?
    ---
    Paul S. Smith
    technetic | design & code

  12. #12
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - nearly there I think.

    That's working now in that the link is changing.

    I added in the DW log out behaviour to log the user out, so the PHP at the top now looks like :

    Code:
    <?php
    //initialize the session
    session_start();
    
    // ** Logout the current user. **
    $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
    if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
      $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
      //to fully log out a visitor we need to clear the session varialbles
      session_unregister('MM_Username');
      session_unregister('MM_UserGroup');
    	
      $logoutGoTo = "logout.php";
      if ($logoutGoTo) {
        header("Location: $logoutGoTo");
        exit;
      }
    }
    ?>
    And that's added the line :

    Code:
    <a href="<?php echo $logoutAction ?>">
    Just before :

    Code:
      <?php
    if (isset($_SESSION['MM_Username'])) 
      echo '<a href="logout.php>">Log Out</a>';
    else 
      echo '<a href="login.php">Log In</a>'; 
    //check the session with 
    print_r($_SESSION); 
    ?>
      </a>
    But I'm not sure of the syntax to set the $logoutAction to the logout link?

  13. #13
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    session_register or session_unregister(); are deprecated and they work only with register globals on
    you shoul use:
    PHP Code:
    unset($_SESSION['MM_Username']);
    unset(
    $_SESSION['MM_UserGroup']);
    $_SESSION=array();
    session_destroy(); 
    read this
    my mobile portal
    ghiris.ro

  14. #14
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where would that go? The log out code there is what Dreamweaver creates.

    DW's login code does include :

    Code:
      //declare two session variables and assign them
        $GLOBALS['MM_Username'] = $loginUsername;
        $GLOBALS['MM_UserGroup'] = $loginStrGroup;	      
    
        //register the session variables
        session_register("MM_Username");
        session_register("MM_UserGroup");
    If that helps?

    All it really needs is for the logout action to be triggered when clicking the link - at the moment where it is, it isn't.

    If it was just a straight log out link, it would just look like :

    <a href="<?php echo $logoutAction ?>">logout.php</a>

    So it's just a case of working that in to :

    Code:
    <?php
    if (isset($_SESSION['MM_Username'])) 
      echo '<a href="logout.php>">Log Out</a>';
    else 
      echo '<a href="login.php">Log In</a>'; 
    ?>
    Instead of the <a href="logout.php>">Log Out</a> line.

    But I'm not sure exactly what the syntax would be.

    But something like (but not) :

    Code:
    <?php
    if (isset($_SESSION['MM_Username'])) 
      echo '<a href="<?php echo $logoutAction ?>">logout.php</a>';
    else 
      echo '<a href="login.php">Log In</a>'; 
    ?>
    or

    Code:
    <?php
    if (isset($_SESSION['MM_Username'])) 
      echo '$logoutAction'>logout.php</a>';
    else 
      echo '<a href="login.php">Log In</a>'; 
    ?>

  15. #15
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your Dreamweaver version is old.
    You will need to forget dreamweaver and start writing php
    my mobile portal
    ghiris.ro

  16. #16
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    I think your Dreamweaver version is old.
    You will need to forget dreamweaver and start writing php
    DW is MX 04.

    It must be quite close tho' - the log out code works on it's own, and the If..else... code is working to alter the link.

    So it's just a case of them both working at the same time.

  17. #17
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Downloading a trial of CS3 to see what that has to offer....

  18. #18
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - think I've figured out a work around.

    For a start, I had a typo at :

    Code:
    echo '<a href="logout.php>">Log Out</a>';
    As that bit was working, rather than add the log out script in there, I've created it on the logout.php page, and set it to log out when the page loads, rather than on the click of a link.

    So effectively instead of

    Click link > log out > goto logout.php

    Its

    Click link > goto logout.php > log out > goto loggedout.php

    Which seems to work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •