This question already has answers here:
PHP: Check if a file is loaded directly instead of including?
(15 answers)
Closed 8 years ago.
I want to make sure that my pages are being included working index page. I would like to know what would be correct way of assuring that my page is being included instead of rendered by itself?
Right now I'm checking if there are at least 2 included files, but I'm not sure I'd it's behavior.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Included page and how I check is it's included
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
I'm not sure if you're talking about this:
include 'test.php';
If yes, then do a simple test like this:
test.php
$testVar = '1';
index.php
include 'test.php';
echo $testVar;
I have no idea what library you're using, so i hope this simple example will allow you to understand.
Related
This question already has answers here:
How to get the previous url using PHP
(6 answers)
Closed 2 years ago.
I have added This Code in My Domain2 where i want this code to be executed Running If it From domain 1
<?php if(isset($_GET['id2'])) { $id2=$_GET['id2']; echo "www.xxxxxxxxxx"" ></iframe>";}
else
{
echo "Not Found";
}
I try this code but not working
If anyone direct accesses this https://Domain2.com/xxxxxxxx/?id=xxxx Hidden part of is showing
How to make hidden if it not Redirected By https://Domain1.com/away.php
You can use $_SERVER['HTTP_REFERER'] to make sure that the refering page (previous page) was the one that you expect:
<?php
// if the request comes from a file that contains the string "conforming.php" then render the page
if(stristr($_SERVER['HTTP_REFERER'], "conforming.php")) {
//serve page if came from conforming.php
}
// if the referring page is not conforming.php, then redirect the user to the conforming version
else {
header("Location: conforming.php");
}
?>
additional information: https://www.w3.org/TR/WCAG20-TECHS/SVR3.html
EDIT**
I customized the code below according to your additional requirements:
<?php
if (stristr($_SERVER['HTTP_REFERER'], "https://Domain1.com/away.php"))
{
if (isset($_GET['id2']))
{
$id2 = $_GET['id2'];
echo "<iframe frameborder=\"0\" scrolling=\"no\" width=\"560\" height=\"320\" src=\"https://www.xxxxxxxxxx.com/files.php?v=$id2\" allowfullscreen=\"true\" webkitallowfullscreen=\"true\" mozallowfullscreen=\"true\" ></iframe>";
} else{
echo "id is empty";
}
}
else
{
//invalid referer! go back to domain1 to make it valid!
header("Location: https://Domain2.com/xxxxxxxx/");
}
?>
What you are looking for is exit;
<?php
if(isset($_GET['id']))
{
$id1=$_GET['id']; echo "xxxxxxxxxx=$id1";}
else {
echo "stop";
exit;
}
?>
Exit will terminate the execution of the current script. This will then continue with the execution processes, which will call your next function.
More reading here: https://www.php.net/manual/en/function.exit.php
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 trying to use an if/else statement in PHP. Currently what I am trying to do is if the $_SESSION['usr']; is equal to the current directory ($dir_auth2) variable that the user is trying to access. Then they can access the directory or index.php I have in it. Else, if the $_SESSION['usr']; is != to the current directory, then redirect them to home page. Currently, when a user types in somebody else's directory, that is not theres they can access it.
<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
//This if statement below is the problem
if($_SESSION['usr'] == $dir_auth1) {
//This demo.php is the home page
header("Location: demo.php");
} else {
echo "You are logged in as " . $dir_auth1;
}
$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
echo $_SESSION['usr'];
echo $dir_auth1;
$dir_user = getcwd();
$dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);
?>
Either you haven't posted the whole script or you don't define $dir_auth2 anywhere. Which is bad since you rely on its value in
if($_SESSION['usr'] == $dir_auth2) {
Also, you should use die() after calling header()
header("Location: demo.php");
die();
How to make a redirect in PHP?
I think this is what you're looking for.
You need to define the variable $dir_auth1 before trying to use it in the if/else statement.
Also I think what you want is != instead of ==
<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
$dir_user = getcwd();
$dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);
if($_SESSION['usr'] != $dir_auth1) {
header("Location: demo.php");
} else {
echo "You are logged in as " . $dir_auth1;
}
?>
Also you can combine all of your string functions into one like so:
$dir_auth1 = str_replace(array("/home/pophub/public_html/","/home/pophub/public_html/gallry/"),"",getcwd());
The following code presents a way that I am currently rendering my pages through index.php. The problem is that I'm not sure how to re-think this so I can pass a page title before the template has been included.
How other way I could do this? This is just my index page, please ask if more code needed.
include($cms->GetTheme() . "/head.php"); This should get the Title information before being included, but I'm not sure how to pass data there from later included page.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Example page being included. The $this->SetTitle($module_name); I would use to set the page title.
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
There are echos all over the place. Try and limit the places where you do that by storing the output, rather than printing it all out straight away.
In your module for example, you could do $this->content = "You have been logged out..."
Then you can change the order of execution:
$cms->IncludeModule($_GET['page']);
include($cms->GetTheme() . "/head.php");
echo $cms->content;
include($cms->GetTheme() . "/foot.php");
until today I've been using following function
function menu_active($pagename)
{
$active = basename($_SERVER['PHP_SELF'], ".php");
if ($active === $pagename) echo "id='active' ";
}
And in HTML
<?php menu_active('page_name'); ?>
to detect which page is active to mark it as current page, but when I changed my page display method it doesn't work anymore. So I wonder how to change the function to make it work. I've tried to declare $pagename as filename.inc.php in the script below, but no use.
I'm using this script to display pages
$pages_dir = 'pages';
if (!empty($_GET['p']))
{
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p . '.inc.php', $pages))
{
include ($pages_dir . '/' . $p . '.inc.php');
}
else
{
echo 'Error';
}
}
else
{
include ($pages_dir . '/home.inc.php');
}
I appreciate any help, feedback or logic and ideas of how to do this right.
Instead of checking which script is running, check $_SERVER['REQUEST_URI']. This will give you the relative URL as requested by the client.