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…
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?
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.
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?
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