Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
Related
I'm having an issue with something quite simple and I have no idea why.
Need a fresh pair of eyes.
In my configuration file I am pulling a users profile information, if their profile is incomplete and they are not in the: home, settings, logout, profile error page then they will be redirected.
Basically I am making it mandatory or they won't be able to navigate to other areas of the system.
$link = $_SERVER["REQUEST_URI"];
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
I've tested my counter which seems to be working fine.
Any help would be appreiated!
$_SERVER['REQUEST_URI'] contains the full pathname of the file from the URL, e.g. /home.php or /folder/logout.php. So none of your $link tests will work. Try:
$link = basename($_SERVER['REQUEST_URI'], '.php');
to get just the filename without the .php extension.
Hi try to debug your code first echo $link and $counter both or add else to see whats wrong
$link = $_SERVER["REQUEST_URI"];
//echo $link; echo $counter;
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
else{
echo "there is some error"."</br>";
echo $link; echo $counter;
}
Comment your condition and test the header location, if the header location does not work, check your condition if don't have something error.
Managed to get it working.
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings"){
header('Location: profileError');
kill();
}
}
Not sure why splitting the IF statements helped but at least it works.
You should have used && condition operator instead of || (OR).
Translated : If the page is not called X and Y then ... Otherwise, If the page is not called X or Y then... it would always be called.
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" && $link !="logout" && $link !="profileError" && $link !="profileSettings"){
header('Location: profileError');
kill();
}
}
I'll try to make a script if the user is redirected to
http://example.com/?url=http://badsite.com
that the script reacts at the URL and displays an echo saying "Not secure", just like the Twitter and Google system, they also check if the URL is harmful.
I've found something, but this isn't what I was seaching for. Google couldn't help me, it gave me all vague answers.
<?php
$badsite = "http://badsite.com";
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == $badsite) {
echo "Secure, redirecting you";
} else {
echo "NOT SECURE";
}
?>
It isn't working as it should, it always displays "Not secure", while the link is secure. I think there's a error somewhere, but I need help fining it.
EDIT
<?php
$badsite = "http://badsite.com";
$host = $_GET['url'];
if($host == $badsite) {
echo "Secure, redirecting you";
} else {
echo "NOT SECURE";
}
?>
Ive tried, but it's still not working :(
Let me make the question a littlebit easier, how can I make it so if the url is
http://example.com/?url=Texthere
that there will be an echo on the page with "this is the page of texthere" and if "Texthere" isn't in the URL, that the echo won't be displayed?
Change your $host variable and if statement like that:
$host = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host != $badsite) {
echo "Secure, redirecting you";
}
This worked just fine for me (tested).
You need to include http:// into your variable, and your if statement was wrong.
I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,
<?php
if host == 'http://domain.com/'; echo {
This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
} else {
};
?>
<?php
$match_domains = array('google.com','www.google.com');
if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
echo 'show on my domain!';
}
?>
Using $_SERVER['SERVER_NAME'] we compare it to our desired domain.
We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.
<?php
$domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
$match_domains = array('google.com');
if(in_array($domain, $match_domains) {
echo 'show on my domain!';
}
?>
Since you want the home page why don't you check the file name
if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
echo "<div>Content</div>";
}
I'd like to have my website redirect to the previous page after submitting login info.
I have searched around for this problem
I have echoed the contents of $url and even did strcmp and it evaluates true (not shown here)
Problem: The ELSE statement always evaluates even though $url == mlbmain.php OR course-website.php
Any suggestions?
<?PHP
require_once("./include/membersite_config.php");
echo "</br> </br> </br> </br>";
$url = isset($_GET['return_url']) ? $_GET['return_url'] : 'login.php';
//url now == to /mlbmain.php OR /course-website.php
$url = substr($url,1);
//url now == to mlbmain.php OR course-website.php
echo $url; //Just to make sure
$url = trim($url); //trim it to make sure no whitespaces
echo "</br>";
echo $url; //Just to make sure it's still the same
if(isset($_POST['submitted']))
{
if($fgmembersite->Login())
{
if($url == "mlbmain.php"){
$fgmembersite->RedirectToURL("mlbmain.php");
}
else if($url == "course-website.php"){
$fgmembersite->RedirectToURL("course-website.php");
}
else
$fgmembersite->RedirectToURL("index.php");
}
}
?>
After you press the Submit button you are making a POST request and the return_url variable will not be available anymore which was set with a GET request. You could create a hidden input field that will store the redirect_url and submit it with the form.
Since you say
It seems to be going to index.php by default
The problem is probably either with
if(isset($_POST['submitted']))
or
if($fgmembersite->Login())
and not related to $url at all.
I guess it can not find mlbmain.php or course-website.php at the current folder, so it throws 404 not found an probably you managed this error to redirect to index.php
Here is my code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">HANDHELD<br />THERMAL IMAGING</div>';
}
else if($_SERVER['REQUEST_URI'] == '/shop/category/mobile-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level">MOBILE IMAGING</div>';
}
Basically this code displays a different left side site navigation depending on which page you're on in the site. It detects what page you're on by the URL of the page by using the PHP REQUEST_URI feature.
My question is this, how can I make the code detect multiple URLs for the same navigation piece?
I tried this code...
if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/' , '/shop/iphone-thermal-imaging-adapter/')
{
But that code doesn't seem to work. Basically all I'm trying to figure out here is how I can use multiple URLs for the REQUEST_URI 'if' statement. Thanks in advance!
Put the URLs in an array and then...
if(in_array($_SERVER['REQUEST_URI'],$array))
You should... switch to a switch statement, which is both neater and offers this option:
switch($_SERVER['REQUEST_URI']) {
case '/shop/category/handheld-thermal-imaging/':
// THERE IS NO CODE AT ALL HERE!
// The absence of a "break;" causes control to "fall through" to the next
case '/shop/iphone-thermal-imaging-adapter/':
echo "something common for the thermal imaging urls";
break;
case 'some other url':
echo "something for the url that stands alone";
break;
}
You can use an "OR" statement in your conditional:
if($_SERVER['REQUEST_URI'] == "whatever" || $_SERVER['REQUEST_URI'] == 'something else")
{
}
I used the OR statement for multiple URL checking
<?php
if ((strpos($_SERVER['REQUEST_URI'], 'contact.php') || strpos($_SERVER['REQUEST_URI'], 'register.php') || strpos($_SERVER['REQUEST_URI'], 'login.php')) === false) {
echo "show";
} else {
echo "hide";
}
?>