hiding links from users who are not logged on php/mysql - php

I am wondering how I can hide two links in my main.php page so when i am not logged in, i can't see the links on my main.php page. Below is my code and basically even when I am logged off, if I type "localhost/main.php" I would still be able to click on those links ( links being "» Logout" and " » Creation". How can i make it that when i am logged off, those links aren't visible or clickable to me. thanks in advance. Below are the two codes in my php file:
» Logout
» Creation

When you are logged in I hope you are setting SESSION. If you are setting a session when you logged is $_SESSION['id'] = "SOME VALUE" then you have to do like this:-
if(isset($_SESSION['id']))
{
echo '» Logout
» Creation';
}
The above code explanation :-
When you click on logout link you have to destroy your session. So when the user is not logged in then it will not get any session, so you have to put a condition if the session is set then only show those link other wise don't show it

Related

Session only registering when set directly from the login page

<?php
session_start();
if(isset($_SESSION['login_user'])) {
require_once('logged.html');
} else {
require_once('notlogged.html');
}
?>
This code works fine when I login directly from the login page, but when I start from the homepage, then a href to the login page, login, it still shows the notlogged.html.
But when I start from the login page, I can login and get to logged.html, go to the signout page and destroy my session, which lands me to the notlogged.html, then login again to the logged.html.
So basically, it works perfectly when I enter from login.php, but not when I enter from index.php. Any idea why this might be?
question isn't clear, 1st of all check whether session created while login from any page, its simple after creating session variable, make alert using JS
alert("$_SESSION['login_user']");

Multiple User Section [more than one page]

I'm starting a session and redirect after successful login to a home.php
now my question is how can i let access the users more pages. i thought about:
<?php
session_start();
if(isset($_SESSION['user_session'])!="")
{
header("Location: home.php");
header("Location: home2.php")
}
?>
this page should only useable to the users. First the user should be redirected to home.php and then the user should get access to another page like home2.php but a non-user shouldn't get access to this page.
When the user is at home.php i thought about a simple <a></a> redirect with html to home2.php
Create a session and make an <a></a> Tag that only registered useres can see.
You have to session_start() in every site and Theo redirect if the User is already logged in

set display by role programmatically in Drupal 7

I want to display this text link, <p>Click here to register.</p> in the body of a larger block of text on a page and but I want the link to show up when some is logged in. I want to use php conditional to show this other link when someone is instead anonymous on the site,
<p>You need to login before you can register. Please Click here to login.
I want a code like:
<?php
if user=="logged-in";
echo "<a href ='register'>Click here to register.</a>";
else echo "<a href ='login'>Click here to login.</a>"
?>
I know I must not have written perfect php but I do not have problems with the php, what I need is the Drupal syntax for if user == "logged-in" and if user == "not-logged-in".
I do not want to use a block and start setting visibility by role. I want to use code as described.
Thanks.
See the docs
You want something like this. Both of your link descriptions need some work though. What you'd most likely want to do is display both the register and the login link to anonymous users.
<?php
if (user_is_logged_in()){
//user is logged in
}
else{
//user is not logged in
}
?>

Homepage not immediately recognizing session PHP

I have a site that displays two different versions of a navigation section depending on if a user is logged in or not.
<?php
if(isset($_SESSION['myusername'])){
echo 'Log Out';
}else{
echo 'Sign Up';
}
?>
The problem happens when a user is logged in and then closes the browser without logging out (and assuming they don't clear cache/cookies on browser exit).
When they open their browser later and come back to the site, the navigation displays as if they're not logged in. If they then click a link elsewhere on the site, i.e. My Account, the navigation then changes to show that they are logged in.
Any ideas what could be causing this? I'd like the navigation to show that they're logged in immediately upon coming back to the site.
First thing, check session_start() appears on your pages before any html, even the !DOCTYPE rule.
Now, on your index page add this:
<?php
session_start();
if(isset($_SESSION['username'])){
header("location: home.php"); // or whatever page you want your users to be redirected to...
}else {
?>
// here your html page should start
<html><head></head><body>
// all the DOM elements on your page
</body></html>
<?php
} // closing end of the else block started above
?>
Must be as below.
ob_start();
session_start();
//code to check session and other
ob_start() is for omitting header already sent error.

Display Admin Link On Every Page

My question is about how do I place a link on the side of every page that leads to the admin page once someone has logged on to my application successfully? I have a sample site I just built, and I would like a link to the admin page available in the navigation column to the right of the page which is displayed site-wide. But if the person is not logged in, they don't see the link, but will continue to see the usual links.
My background is totally different from web development, so forgive my stupid question.
I'm using PHP and MySQL for the application.
Without seeing how you display your menu or what key is used for the session, Ill assume some things:
<?php
session_start();
// do your login stuff and set session as logged in
$_SESSION['logged_in'] = true;
?>
Then in your menu or how ever you display it:
<?php
//navigation column
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true){echo 'Admin';}
//navigation column continue with rest of links
?>
Or the ternary operator assign link to a variable
<?php
$adminLink = (isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true)?'Admin':'';
echo $adminLink;
?>
You should use a session variable to track the user's session and see if they are logged in.
if(isset($_SESSION['id'])) echo 'Admin Area';

Categories