Skip to content Skip to sidebar Skip to footer

Php Method To Hide Link Until User Logged In

I used this to hide links until after the user is logged in, and was just wondering if this will cause any security issues or other issues in production code? I have been testing i

Solution 1:

There is no problem in this code until you put a session check also in the file

if session id is not set then send them back to home page.. Because if user knows the URL then they can navigate to the link

Solution 2:

Make sure to add a function which will redirect the users to the login page as soon as the session gets destroyed i.e logout.

Also, as mentioned by @Saeed Ansari, add some logic to your project so only the login page is rendered when there is no active session or the user is not logged in.

HTH.

Solution 3:

Either way, if your solution is to simply 'hide this link' until the user has logged in, this is not constructive code.

You should have a user object or user $_SESSION identifier registered in the session for when the user logs on.

For example. User logs on, you set a flag $_SESSION['Username'] = "Bob", where Bob is the user's username.

Then in your code, you could do something along the lines of:

if(array_key_exists('Username', $_SESSION)) { echo'<a href="./ewo.php" target="_self"> EWO </a>'; }

Then when a user logs into your site successfully, register their username (atleast) in the $_SESSION, ie

$_SESSION['Username'] = 'Bob';

It is a good idea to have full control over your session by using session variables, rather than just relying on if a session has an ID.

It is never safe to assume, so I would also recommend (if you haven't done so) checking in the ewo.php file for the same thing ... check if the session has a registered Username/etc and if not redirect header('Location: /'); for example, to redirect the user back to the home page.

Solution 4:

You could do it via a Session.

If you wanna check if the variable is set (User is logged in) in the session use:

<?php
session_start();


if (isset($_SESSION['username'])) {
   echo"Your link here";
} else {
   echo"login first";
}
?>

Post a Comment for "Php Method To Hide Link Until User Logged In"