Reputation: 3225
I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
Upvotes: 5
Views: 18457
Reputation: 515
You can use PHP if-else
condition and write HTML like this:
<a href="" onclick="return false;">
Upvotes: 0
Reputation: 6689
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo '<a href="actual_link.html">LINK</a>';
}else{
echo '<a href="#">LINK</a>';
}
that will keep showing the link but will lead nowhere if the user is not logged in. Or you can do : if(isset($_SESSION['id'])){ echo 'LINK'; }else{ //do nothing here or put a link to the login page } that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it. Note that code in this answer is just a guess of your real code
Upvotes: 0
Reputation: 624
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
Upvotes: 0
Reputation: 9635
try this
if($php_var == "no")
{
echo '<a href="javascript:void(0);">Your Text For Link</a>';
}
else
{
echo '<a href="your link">Your Text For Link</a>';
}
user javascript:void(0);
for no redirection. this will maintain your css for link like others but when you click it won't redirect.
Upvotes: 5
Reputation: 368
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
Upvotes: 2
Reputation: 4607
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
Upvotes: 1
Reputation: 3582
You would need to do the processing pre-output, PHP will not dynamically disable the href
of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo '<a href="' . ($php_var == 'no' ? '#' : 'actual_link.html') . '">Link</a>';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
Upvotes: 1
Reputation: 3953
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "<a href="your.link">This is the text of your link.</a>";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
Upvotes: 2