InsaneCoder
InsaneCoder

Reputation: 8288

How to access all links of a particular class on the webpage using PHP

I have around 500 links on my webpage and if the user is not logged in and clicks on them I want to alert() him to log-in first.

So I kept all of them in a class restricted_link and now if the user is not logged-in I want to modify it as

<a href="javascript:void(0);" onclick="alert('Login Please');">Text For Link</a>

Otherwise,

<a href="link" >Text For Link</a>

Now ,I really don't know how to cycle through all those links using PHP

Secondly,if there is a better way possible then please suggest that.

Upvotes: 1

Views: 39

Answers (1)

Vlad Nikitin
Vlad Nikitin

Reputation: 1951

You can do the next way, basic idea:

<script type="text/javascript">
function loginStaff () {
<?php
if (!$logedin) {
    echo 'alert("Login Please"); return false;';
}
?>
}
</script>

...................

<a href="/always/original/href" onclick="loginStaff();">Text For Link</a>

Upvotes: 1

Related Questions