简体   繁体   English

使用PHP禁用href链接

[英]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' 如果PHP变量= '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? 有没有办法使用CSS或Javascript来禁用链接?

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); 用户javascript:void(0); for no redirection. 没有重定向。 this will maintain your css for link like others but when you click it won't redirect. 这将保持你的css链接像其他人一样,但当你点击它不会重定向。

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. 您需要进行处理预输出,PHP不会动态禁用已创建的DOM元素的href

If you are producing the output of the links via PHP, you could do something like: 如果您通过PHP生成链接的输出,您可以执行以下操作:

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. 否则,您可以创建对PHP脚本的AJAX调用,如果它返回“否”,则遍历您的页面链接并通过JavaScript禁用链接。

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

this will remove all href from a tags. 这将删除标签中的所有href。 If php var is no. 如果php var为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: 如果没有显示链接,我还会添加一些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'; 如果用户没有登录,它将继续显示链接但是无处可去。或者你可以这样做: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. } else {//在此处不执行任何操作或将链接添加到登录页面},只有在您登录时才会显示链接。

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;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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