Can use parse_url
to check the $_SERVER['REQUEST_URI']
Note 1: the mistake:
"Uncaught Referenceerror: $ is not defined at (index):36 (Anonymous) @ (index):36"
Indicates that jQuery has not been loaded on your page, it is necessary to put it before the script you want to run, something like:
<html>
<head>
<script src="pasta/jquery.js"></script>
<script src="pasta/fancybox.js"></script>
</head>
<body>
...
<?php if (<condição>): ?>
<script type="text/javascript">
$(document).ready(function() {
$.fancybox.open({
src : '/assets/images/banner-aviso.png',
type : 'image'
});
});
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png"> </a>
<?php endif; ?>
...
</body>
</html>
Note 2: the REQUEST_URI
you must have the apostrophes (single quotes), because if you do $_SERVER[REQUEST_URI]
the PHP will first look for a constant call REQUEST_URI
, which does not exist, so will issue a notice thus:
Notice: Use of Undefined Constant REQUEST_URI - assumed 'REQUEST_URI'
The assumed 'REQUEST_URI'
indicates that PHP for not finding the constant has cast a string, however note that although $_SERVER[REQUEST_URI]
work, but perhaps some additional script generate the constant REQUEST_URI
with some value, so this will fail for sure.
Note also that strpos()
can return 0
or false
, in the case of @DVD script no, still if you are accessing a page with the similar name foobarhome.php
the strpos
will also enter the IF
, then the ideal to avoid problems is to use the parse_url
or else use preg_match
, follows examples:
Using parse_url
The parse_url($url, PHP_URL_PATH)
will extract only the path
, if in the home the value /home
<?php if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === '/home'): ?>
<script type="text/javascript">
$(document).ready(function() {
$.fancybox.open({
src : '/assets/images/banner-aviso.png',
type : 'image'
});
});
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png"> </a>
<?php endif; ?>
If the home you refer to is index.php, then do so:
<?php if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) === '/'): ?>
...
If it is a page range, you can use an array:
Note: the ltrim
take the bar from the top
<?php
$pathAtual = ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$permitidos = array( 'home', 'contato', 'admin/foo' ); //Adicione as páginas permitidas aqui
?>
<?php if (in_array($pathAtual, $permitidos)): ?>
...
With preg_match (regex)
In this case I used regex (regular expressions), with it you can create a range of pages within the regex, for example this regex will check if it is the home or the index #/(index|home)$#
, example:
<?php if (preg_match('#/(index|home)$#', $_SERVER['REQUEST_URI'])): ?>
<script type="text/javascript">
$(document).ready(function() {
$.fancybox.open({
src : '/assets/images/banner-aviso.png',
type : 'image'
});
});
</script>
<a class="hidden-link pop-up" href="/assets/images/banner-aviso.png"> </a>
<?php endif; ?>
jQuery is not being loaded.
– Sam
How I would do a PHP check to see the URL?
– Felipe Viero Goulart
But then how would I make him open the Fancybox after the check?
– Felipe Viero Goulart
Yes. There are two files: home.php and index.php - index.php goes to all pages.
– Felipe Viero Goulart
@Felipestoker the URL has the format
http://site.com/home
orhttp://site.com/home.php
orhttp://site.com/?pag=home
?– Guilherme Nascimento