I have the following code:
<?php
$path=$_SERVER['HTTP_HOST'].$_SERVER[REQUEST_URI];
$test=file_get_contents('http://www.domain.com/test.php?i='.$path);
echo $test;
?>
I want to fetch and insert a specific section of the test.php file depending on the $path variable (the url from which the test.php file is accessed.)
How do I do this? Am I on the right track? I don't know how to set up my text.php file so that the function echos the correct section for the webpage it is accessed from. How do I do this? Or is there a better way? (test.php is not a local file)
Thank you!
It seems to me that you are trying to display dynamic information (such as html or text) on test.php based upon whichever script accesses it. From there, you would like to take the information from test.php and relay it back to index.php by extracting the source code and echoing it? Correct me if I am wrong.
This is achievable using $_GET variables. You were already heading in the right direction by adding the ?i= to the end of the URL.
Something like this should help you roughly achieve what you want:
(test.php)
if (isset($_GET['i]') {
switch ($_GET['i']) {
case 'http://www.domain2.com/index.php':
echo 'You\'re on domain2.com';
break;
case 'http://www.domain3.com/index.php':
echo 'You\'re on domain3.com';
break;
}
}
index.php should work fine how it is, unless the data stored in your variable regarding the path to the page you're currently on is incorrect.
Related
the dilemma I have is my website index.php calls to a template php file on a button press like this:
case 'main':
$page = getTemplate('main.php', array('user'=>$user));
echo $page;
break;
This main.php template file is in a folder in "/var/www/template/" How do I stop people going to: domain.com/template/main.php and viewing the code for that page. I think the solution would be to make the localhost be able to pull the it and display it rather than the user or something along those lines. Any help would be appreciated thank you.
Like a comment said, the PHP file will not be printed, it will print the HTML result that the php file produce.
Maybe it produces some errors indicating vulnerabilities to a potential attacker ? If that's your case, you should handle this directly into the php code or use a .htaccess at the root of your site. You can't find some help there.
How to deny access to a file in .htaccess
Managed to fix this by putting this at the top of the php page I wanted to render:
<?php
if (!isset($_GET['page'])) {
header('Location: /main');
exit();
}
?>
This means if someone goes "domain.com/template/main.php" to attempt to view the source code, it will redirect them back to the main webpage for my site. Thanks for your suggestions however.
I would like to include at the beginning of my script a PHP file that open a IF condition. Then i write my script, and to finish I include another PHP file that close the conditon.
This bring me to a "Parse error: syntax error, unexpected end of file in ..." error.
This will be better to understand with this simple example :
header.php
if(aConditionalTest()) {
footer.php
} // endIf
mypage.php
include_once 'header.php';
echo 'my awesome content';
include_once 'footer.php';
FYI: I would like to do this for example :
to check everywhere that a user is authorized before displaying the content
implement a webpage caching system (see http://www.phpfastcache.com/ in "Example" section, "Caching Whole Webpage")
THANKS!
edit : Explain more precisely WHY I want to do this for using phpfastcache :
http://www.phpfastcache.com/ says :
Caching Whole Webpage PHP Cache whole web page :
You can use phpFastCache to cache the whole webpage easy too. This is simple
example, but in real code, you should split it to 2 files:
cache_start.php and cache_end.php. The cache_start.php will store the
beginning code until ob_start(); and the cache_end.php will start from
GET HTML WEBPAGE. Then, your index.php will include cache_start.php on
beginning and cache_end.php at the end of file.
That's just what I try to do!
According to their piece of code below, this brings to the situation where the condition is opened in "cache_start.php" and then closed in "cache_end.php"
cache_start.php
use phpFastCache\CacheManager;
$cache = CacheManager::Memcached();
$keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
// try to get from Cache first.
$resultsItem = $cache->getItem($keyword_webpage)
if(!$resultsItem->isHit()) {
ob_start();
cache_end.php
// GET HTML WEBPAGE
$html = ob_get_contents();
$resultsItem->set($html)->expireAfter(1800);
$cache->save($resultsItem);
}
echo $resultsItem->get();
mypage.php
include_once 'cache_start.php';
// my awesome content to cache goes here...
include_once 'cache_end.php';
myotherpage.php
include_once 'cache_start.php';
// my other great content to cache goes here...
include_once 'cache_end.php';
So the reason WHY I want to put the phpfastcache code in 2 separate files is that I have many different PHP pages to cache, so I would like to avoir repeating all this code on each page...
Hope this edit will help you better understand why I would do that, even if I understood, as I feared, that is is not possible.
Give it a try:
how can I achieve this ?
Do it the evil way and eval all instead of including :) Like
eval(file_get_contents('header.php').'<?php echo "my awesome content";?>'.file_get_contents('footer.php'));
That can be a solution, if you want to join the dark side :)
SideNote: In this solution, you have to keep an eye on global variables!!
But please, thing about the fact, that you want to spread conditions over seperate files, what in my opinion is very very very bad practise.
Did i really answer this 8]
I try it in other way.
Only rule: works only in global space (where else :-))
So you want to open an if() in cache_start.php and close it cache_end.php. (for ob_cache reasons)
But if the condition isn't changed why not doing the condition twice!
In each file test for if(condition)!
Or set up an variabale like $cach_op_started=true and test for it in the second if() in cache_end.php
Thing boths should work for you.
Its a little funny that i didnt see that solution at the first time :)
Last Note:
You can also use auto prepend and append files in PHP if you want to.
That can be configurated in php.ini.
The files will automaticly loaded before and after an script, always.
http://www.webdevsecrets.com/using-phps-auto_prepend_file-and-auto_append_file/
Have a nice time.
So I understand that the problem is that when you don't want a certain user to see the contents of a page, the rest of the page isn't loaded and that is whats causing the error?
If so, why don't you just always include the files and in the specific page you set the conditions for who can view what?
I use ob_start() in my header, and ob_end_flush in my footer which works great.
and then check with my SESSION variables on the specific page's content if the logged in user has the right to see the content, else display a message like:"You are not authorized to see this content"
So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.
Hi guys consider I have a .php page that I need to include somewhere else.
page.php:
<?php
dosomecmd();
doothercmd();
//etc
?><html>pagehtml</html>
Is there a way to include this page but not printing the html inside? (I just need to execute those command)
I could do with ob
ob_start();
include('page.php');
ob_discard();
But i would prefer a faster way.
Thanks
Edit: i know i can sepearate html and php (and i already do) but that page.php is non else than a "static" cache I make, but sometimes I need to execute some command inside that cache instead to printing automatically it out
Edit2: of course i don't need everyime to not output the html (otherwise I could just delete all html) I need to return; only based on the results of my cmds up there
Thanks all i find a solution (add return;)
Would it be easier to have a common page with only the php that you need to share, and include it in both page.php and your other page?
you can try to seperate with
$_SERVER['REQUEST_URI']
also I wouldn't recommend that
Try to separate the PHP logic at the start of your current script into another file, then include that into both of your scripts.
I have a PHP script (index.php) that changes the page shown depending on the query part of the URL (using GET). All the links in the page access different "pages" using ?page=pageName and the index.php script loads HTML from text files accordingly (echoing it to the screen).
So, if a user goes to index.php?page=about, my PHP script loads a file called about.php and echoes it in a view container in the browser.
However, I have a big problem: The content I'm loading has PHP scripts inside it, but those scripts aren't executed when echoed using the index.php script. That makes it impossible for me to load a page that contains, say, a directory listing pulled from a MySQL database.
So basically, I want to know if there is any way that I can echo a PHP file onto a page but execute the PHP code inside the file as well.
For clarity, I have attached an annotated screenshot that describes what I mean:
EDIT: Here's the link:
Use if statements to include('about.php') in index.php, and put the echo statements inside about.php. If this doesn't work, please post code examples.
if( $_REQUEST['someQuery'] == 'about' )
{
include('about.php');
}
Using include() will include the content of another PHP script and execute it. If that other PHP script contains functions that produce the output then you will need to invoke them separately.
Please don't start your sentences with conjunctives.
changes the page shown depending on the URL
Doesn't that happen with most web pages?
access different "pages" using ?page=pageName
Oh - you mean by the query part of the URL - a front controller pattern - yeuch!
I can echo a PHP file onto a page but execute the PHP code inside the file as well.
Do you mean you want to see the PHP source code and the output of the code? Or just the output of the code?
In your front controller:
<?php
if (realpath($_REQUEST['page'])!=$_REQUEST['page']) {
header("HTTP/1.1 500 Internal Server Error");
die ("Naughty!");
}
$script=SOME_PATH . $_REQUEST['page'] . ".php";
if (!file_exists($script) || !is_readable($script)) {
header("HTTP/1.0 404 Not Found");
die ("File not found");
}
print "src = <br />";
highlight_file($script);
print "<hr />Output:<br />";
include($script);