I'm trying to right a simple if statement to basically say that if the user is on page_A then echo "i am on page A"; and if I'm on page_B then echo "i am on the page B"; and I'm using the PHP function $_SERVER['REQUEST_URI']; to check if they match what is in my string that is stored in the variable $page_A and $page_B.
my code is as follows:
<?php
$page_A = "/test.co.za/voice/";
$page_B = "/test.co.za/artistes/";
$match = $_SERVER['REQUEST_URI'];
if ( $page_A == $match ) {
echo "i am on page A";
}elseif ( $page_B == $match ) {
echo "i am on the page B";
}
?>
Where am i going wrong or is there another way i can achieve getting this to work?
Thanks team :)
if(strpos($_SERVER['REQUEST_URI'],'voice')!==false){
echo "i am on page A";
}elseif(strpos($_SERVER['REQUEST_URI'],'artistes')!==false){
echo "i am on the page B";
}
I don't quite understand how you are doing this, if you have 2 different pages you always know at which page you are.
If you are using htaccess to redirect the request to a unique file, you should do something like this:
Add this line in htaccess:
RewriteRule ^([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?([0-9a-zA-Z-_]*)\/?$ index.php?p=$1&s=$2&d=$3&ID=$4&%{QUERY_STRING}
Now, if you open for instances: http://test.co.za/voice/ this will be redirected to /index.php.
At index.php you can read:
$page = $_REQUEST['p'];
if ($page == 'voice') { //do stuff }
Related
In OpenCart 2, I am editing the appearance/php of the header only in the "success"/"thank you" page (catalog/view/theme/*/template/common/success.tpl).
So, in catalog/view/theme/*/template/common/header.tpl I want to do something like:
if( $is_thank_you_page ){
echo "stuff";
// bonus: I wanted to get the order email but maybe it should be a different post
}
But how can I check in the header.tpl if it is the "success"/"thank you" page?
I tried setting a variable in success.tpl before printing the header with no results.
You could try something like this (go about it based on your URL):
<?php
$parameters = explode('/', $_SERVER['REQUEST_URI']);
if(end($parameters) === 'success.tpl'){
//the condition with $parameters depends on the exact look of your URL
//you could also access an index directly
}
Basically, it takes the REQUEST_URI (part after the domain), splits it around the / symbols and then checks if it ends with success.tpl
You could also make a switch for the end($parameters) instead of the if.
I don't know opencart structure, but if this value never change you can try with strpos/stripos, something like:
if(stripos($var_with_page_title, 'thank you') !== false) {
do_something();
}
If you want you detect checkout/success page in you header, do following:
open catalog/controller/common/header.php
find
// Menu
$this->load->model('catalog/category');
Add before
// success page checking
$data['success'] = '';
if (isset($this->request->get['route']) && $this->request->get['route'] == 'checkout/success') {
$data['success'] = true;
}
// looking for email from the order
$data['success_email'] = '';
if ($this->customer->isLogged()) {
$data['success_email'] = $customer_info['email'];
} elseif (isset(this->session->data['guest']['email'])) {
$data['success_email'] = $this->session->data['guest']['email'];
}
Now in catalog/view/theme/YOUR_THEME/template/common/header.tpl
add anywhere you like
<?php if ($success) { ?>
//do something
<?php if ($success_email) { ?><?php echo $success_email; ?><?php } ?>
<?php } ?>
With bonus email
I am a beginner when it comes to php, and I have encountered a problem that I cannot find the solution for. I have tried searching for a relevant answer but I haven't found one.
I have the following code in my index.php:
<?php if ($set_status = 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
and this a bit further down:
<?php include 'scan/a.php'; ?>
And inside my a.php I have the following code:
<?php
$a = file_get_contents
('http://www.a-random-website/text.html', NULL, NULL, 2, 1);
if ($a == "0") {
include 'fail.php';
} elseif ($a == "1") {
include 'success.php';
} else {
echo 'Offline';
}
?>
And inside my fail.php I have the following code:
<?php
$set_status = 2;
echo 'Failed';
?>
So the idea here is that "a.php" will fetch a number from the website (The correct website has either ["1"] or ["0"] displayed that the code will fetch).
Depending on the result it returns, a.php will include either "fail.php" or "success.php", each containing either a success or a fail-message. If file_get_contents return a 0 I also want fail.php to $set_status = 2; which will cause "There is one or more errors" to be displayed on the front-page (index.php).
The reason that I am using include is that there's going to be a "b.php" and "c.php" and "d.php" and so on, all doing the same thing but fetching data from different pages. I want the success or fail-message to remain easy to edit, without having to edit each and every new x.php file.
So here's where it gets problematic. Everything works beautifully, except for the "There is one or more errors"-message that's supposed to trigger if ($set_status = 2).
I can get as far as the message showing, but when I switch the 1 and 0 in "a.php" (To simulate a specific result) the message will still show. I can't seem to figure it out.
So my question is: What have I done wrong, and what is the correct way to do it?
Thank you in advance!
Best regards,
Marc
use == operator in index.php to compare
<?php if ($set_status == 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
<?php if ($set_status ==2) {
echo 'There\'s one or more errors';
} else {
echo '';
}
?>
Just change this code ... see slash\ and == in the echo line .. else of your code id fine
Not sure how to explain this but i'm trying to run (2) php if statements together using a getIsHomePage block of code. I need for the div replacement to be true if home page = yes and if "certain page url" = yes. If not either of these two pages then the 'else' statement should be true.
Here is the code block I have now:
<?php if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()) {
echo '<div class="main">';
} else {
echo '<div class="main container">';
}
?>
Any assistance would be appreciated.
Thanks,
Ryan.
You can use the below-mentioned code to check if you are on Home page or viewing your custom URL.
<?php
$myCustomUrl = "...";
if($this->getIsHomePage() || $myCustomUrl ) {
echo 'Either you are on Homepage or your page URL is : ' .$myCustomUrl;
} else {
echo 'Nither you are on Homepage nor your page URL is : ' .$myCustomUrl;
}
?>
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";
}
?>
I would like to know how to create a PHP IF Statement that detects if a user is loading/on a certain URL. In my case it's:
www.design.mywebsite.com
The IF Statement needs to cover the whole subdomain, EG the URL will also have to be detected:
www.design.mywebsite.com/filename.php
Once an IF statement is established, I want to add a PHP variable. So it might help others, let's call it:
$myvariable = "Hey there! You're on my subdomain";
So To Sum It Up:
The final code should look something like this...
<?php
if //Code to detect subdomain// {
$myvariable = "Hey there! You're on my subdomain";
}
?>
A simple solution is
$host = "baba.stackoverflow.com"; // Your Sub domain
if ($_SERVER['HTTP_HOST'] == $host) {
echo "Hello baba Sub Domain";
} else {
echo "not baba domain";
}
You can also use parse_url http://php.net/manual/en/function.parse-url.php if you have the URL
$url = "http://baba.stackoverflow.com/questions/10171866/detect-if-a-user-is-on-a-subdomain-and-then-add-a-variable-using-php";
$info = parse_url($url);
if ($info['host'] == $host) {
echo "Hello baba Sub Domain";
} else {
echo "not baba domain";
}
Please replace $url with $_GET ;