PHP Session

I have a menu that I would like to do this with it. If user is currently logged in show “Log out” link and otherwise show “Log in”. Something like this…

          <?php
             if(isset($_SESSION)) {
                 echo "<div><a href='#'>Log Out</a></div>";
             }
             else {
                 echo "<div><a href='#'>Log In</a></div>";
             }
        ?>  

My question is how would I call PHP function when user clicks on “Log out” link?

The free PHP online manual has numerous examples and this function could be used

http://php.net/manual/en/function.session-abort.php

Or even just $_SESSION = NULL;

Of course. I was just curious if there is a way to trigger PHP function on click of HTML anchor

The key for your question is the href attribute of the anchor tag. Instead of ‘#’ for its value, you can set it to the path of a php file on your server for processing the logout. Or you could add the “onclick” attribute to the anchor tag, setting it to a JavaScript function that could in turn trigger dynamic changes to your page.

Be default: No. The intention behind anchors is just to link to certain parts of the document. You may override this with JS, but what stands asgainst just linking to another page?

linking to what page? I am trying to show this…

<?php
             if(isset($_SESSION)) {
                 echo "<div><a href='#'>Log Out</a></div>";
             }
             else {
                 echo "<div><a href='#'>Log In</a></div>";
             }
    ?>

The hashtag (#) symbol is usually a placeholder for the link. Using a hashtag symbol also typically doesn’t activate anything. If you want a user to log off, you need to provide a link in place of the hashtag symbol that will be doing the logging off part.

Yes I know. I was just hoping there is way to call one single function rather then create whole file (file.php) that will only contain couple lines of the code to clear the session.

To run any php requires a page refresh, so you will be loading a “whole file” however you do it.

You could have (link to) a php script that does nothing more than that one function (log out), then redirects back to the page you came from.

You will have to use a file regardless of what you want. Calling the function requires a file to initiate the state.

ok I fixed this but the problem is that it takes reload of the page for the menu item log out.
I figured I can use header function but unfortunately that function would not be first item to sent out as output. Is there any other way to resolve this?

Please show the codes so that we can understand what you are trying to do.

As I shown in above code (top of the post) I want to show one link if user is logged in and other if user is not logged in. I was able to resolve that with php code in my template but the problem become that I needed to refresh that page in order for this switch to take place.

Show link → log out is user is loged in
Show link → log in if user is not loged in

Since i am sending data from the user login form via Ajax i just used jQuery function replaceWith to switch these elements until user moves onto next page or reloads page which in turns will switch these elements via PHP

I don’t understand what you are trying to ask. I assumed the first posted snippets were resolved because you said

Which I assumed you were referring to fixing the log out page. But then you said

Which is what I am referring to. Please post the codes in the log out page. Also, anything outputted before the header() calls will give you a headers already sent error.

When the user logs in, have PHP save the user’s id number into the $_SESSION array, then when it decides what link (log-in or log-out) to display, you can just use isset() to see if a user id number is present in the $_SESSION array

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.