简体   繁体   中英

disable a href links using PHP

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?

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.

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.

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.

<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>

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>';
    }

?>

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
}

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

您可以使用PHP if-else条件并像这样编写HTML:

<a href="" onclick="return false;">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM