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 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
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 "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
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 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 'Link';
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>
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 'LINK';
}else{
echo 'LINK';
}
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
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">
Related
Basically my task is,through the mandatory use of _GET,I'm supposed to make a code that fetches a specific php file when someone types in something like ..php?page=airlines and I dunno if I'm just lacking information or what but this isn't working router.php:
<?php
$nav =array("home"=>"C:\xampp\htdocs\project\home.php",
"flight"=>"C:xampp\htdocs\project\flight-detail.php",
"order"=>"C:xampp\htdocs\project\order-flight.php,",
"testimonial"=>"C:xampp\htdocs\project\add-testimonial.php");
if ( isset($nav[$_GET['page']]) )
{
echo header('Location: ' . $nav[$_GET['page']]);
}
if i understood your question, there are many ways to reach your goal.
I write an example that no change very your code:
<?php
$myPath="C:\xampp\htdocs\project\";
$nav =array("home"=>"home.php",
"flight"=>"flight-detail.php",
"order"=>"order-flight.php,",
"testimonial"=>"add-testimonial.php");
if ( isset($_GET['page']) and !empty($_GET['page']))
{
$myFile=nav[$_GET['page']];//i assume that in page there are only array keys value
echo header('Location: ' . myPath.$myFile);//i.e. I suppose in page there is home output is: "C:\xampp\htdocs\project\home.php"; file
}
else
{
echo 'error: page not found!';
}
An other "easy" solution is to create in first page the link where the user can choose the page:
<a href="C:\xampp\htdocs\project\home.php">HOME<a>
<a href="C:\xampp\htdocs\project\flight-detail.php">FLIGHT DETAIL<a>
....
Hope this helps
My website has a simple navigation using a styled <ul> list, with the current page's link highlighted. Currently, I do this by giving the <a> object a CSS class like this:
<ul class="bd-nav">
<li>Home</li>
<li>Contact</li>
</ul>
with the corresponding CSS:
.bd-nav-active {
background-color: #563a64;
}
This works perfectly. However, I would like to build the website with PHP and have a seperate file for the header/navigation and then just <?php include ?> that file on every other page.
Is there a way to dynamically set the class of the navigation links, depending on which page you're on? What would be the best approach here?
Solved after some fiddling! I simply put the class attribute into a variable like this: (make sure to escape the quotation marks!)
<?php
$nav_active = "class=\"bd-nav-active\""
?>
Then used that variable in my navigation like this:
<ul class="bd-nav">
<li><a href="index.html" <?php if ($pid == 1) echo $nav_active; ?>>Home</a></li>
<li><a href="contact.html" <?php if ($pid == 2) echo $nav_active; ?>>Contact</a></li>
</ul>
And then on the respective pages, I simply set the $pid variable:
<?php $pid = 1; ?>
Works perfectly! Thanks for the helpful answers!
#Arrabidas92 had a nice way to automatically do this by getting the page URL, but I think I'll be doing it like this to have better control over how the navigation looks.
The menu needs to be dynamically generated and each page needs to have a unique id or something at the top of the page.
When you dynamically generate the menu, insert and if clause that will echo the active class if generated menu item is the same with current page.
I have not coded in php for some time now but i use to do something like this, in each page give a unique file name at the top. For example in the home page:
<?php
$file_name = "index.php";
?>
and then in the included file apply a logic like this:
<?php
if($file_name == "index.php"){
//Your navigation for the home page
}
else if($file_name == "about.php"){
//Your navigation for the about page
}
?>
Alright so I will give you some tips. First, to know on wihch page of your website you are you can use in PHP a superglobal called $SERVER with this attribute : $_SERVER['REQUEST_URI'].
By calling this, PHP is going to return the parts of the url after your domain.com. For example, if the current url was domain.com/home, echo will return only '/home'.
Then, you can place your logic about highlighting your links :
if($_SERVER['REQUEST_URI' == '/home') {
//Add active class to the link HOME
}...
You can try this one, but this is a Jquery, just put the CDN in your file.
$(document).ready(function(){
if(window.location.href === "index.php") {
$(".bd-nav").addClass("active-bgRed");
}
else if(window.location.href === "about.php") {
$(".bd-nav").addClass("active-bgBlue");
}
else if(window.location.href === "contact.php") {
$(".bd-nav").addClass("active-bgGreen");
}
});
// CSS
.active-bgRed{
background-color: red;
}
.active-bgBlue{
background-color: blue;
}
.active-bgGreen{
background-color: green;
}
// JQuery CDN
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
I have a page with two links (text link & banner link),
that should lead to the same redirect page
(on my domain).
The redirect would be to a link that shall include a variable,
that indicates which of the two link was clicked.
e.g.:
<?php
header("Location: http://external-domain.com/?ref=[value]");
die();
?>
wheareas the "value" should be "text" / "banner" or something similar.
How can I do this?
I'm not a web programmer at all so I don't have much technical knowledge,
I guess one possible solution (which I would rather avoid) would be to give a separate id for the text link and for the banner, e.g.:
text link:
http://mydomain.com/redirect.php?id=text
banner link:
http://mydomain.com/redirect.php?id=banner
whereas redirect.php would contain:
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
But that would mean that I have to use a different id for these internal links;
I would rather find a way to use the same exact internal links (maybe for example, have each link in a "div" or a "class" and get the div/class name somehow, and have them appear as the [value].
*EDIT:
Let me emphasize again: I'm looking for a way to do this without having to use any "?id=[something]" at the end of the text link or the banner link.
Thanks!
If you're already outputting the banner/text do this for the banner
<img src="imageHere.png">
And this for the text
text here
Then catch the id get values with
<?php
$id = $_GET['id'];
if ($id == 'banner'){
echo 'The clicked link was a banner';
}
else{
echo 'The clicked link was text';
}
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
should be
<?php
$source = $_GET['id'];
header("Location: http://external-domain.com/?ref=".$source);
die();
?>
Basic concatenation.
Can anybody tell me how can I add a css class to this links when click on it?
This is my links :
echo '<li>Tutor</li>
<li>Institute</li>';
My problem is how I identify which link is clicked by users?
I assume you want to add a class to a link if it is the current link being viewed.
Since each link has a different GET parameter you could use that to identify it.
echo '<li><a '. (isset($_GET['tutor']) ? 'class="current"' : '') .' href="?tutor=link&subject='.urlencode($subject).'">Tutor</a></li>
<li><a '. (isset($_GET['institute']) ? 'class="current"' : '') .' href="?institute=link&subject='.urlencode($subject).'">Institute</a></li>';
Using a pure JavaScript solution you could do:
<a id="tutor" href="?tutor=link&subject='.urlencode($subject).'" onclick="changeClass(this)">Tutor</a>
function changeClass(link) {
if (link.id == "tutor") {
link.className = "current";
}
}
Edit:
You can identify which link was clicked by the link variable passed into the changeClass function. The link variable will include an id property which will tell you the identifier of the a tag.
you can achieve this using javascript or jquery
$(this).css('newclassname')
thats the example of jquery
updated
you can bind it to a function to get the hold of the clicked element
$('a').click(function() {
$(this).css('current');
alert($(this).html()); // this will pop up tutor if its clicked
});
I'm trying to have my website not show certain code if they come from certain URL's.
For example, Wikipedia don't like links to sites that have popups on them. So I need to not show the code for that referer.
I found the following code but it doesn't seem to work when code is places instead of text
<?php $ref=getenv('HTTP_REFERER');
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>
If you want to avoid showing code to google:
<?php if (!strstr(strtolower($_SERVER['HTTP_USER_AGENT']),"googlebot")){ ?>
//Show what you want, google will not see it
}else{
//show other code
}?>?>
For wikipedia:
<?php if (!strstr(strtolower($_SERVER['HTTP_REFERER']),"wikipedia")){ ?>
//Show what you want, wikipedia will not see it
}else{
//show other code
}?>
Enjoy ;)
You were talking about Wikipedia. Probably the problem is that google isn't sending you "google" in their referer string
it must work, try alternative variable
<?php
$ref=$_SERVER['HTTP_REFERER'];
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>