I cant find specific answer for my problem. So what I'm trying to do is to define index page by using only index.php. This is my code so far:
<?php
if(isset($_GET['page'])){
if(!$_GET['page']){
include "template/home.php";
}
if($_GET['page']=="gallery"){
include "template/gallery.php";
}
}
?>
When I'm trying to open it on local host there is nothing only white screen without any errors. Only thing that makes me problem is this string, but still without any errors, can't find what problem is...
if(!$_GET['page']){
include "template/home.php";
}
I was trying to find on internet solution, but there was nothing helpful, hope u can help me :)
The issue is that you're contradicting yourself in your code. Your example reads like this: "If page is set, proceed. Then, if page is not set, include the home page, if page is gallery, then include the gallery page." Do you see the issue? The isset() check will ensure that the code with the negation operator ! is not run.
The solution is to move that code outside of the primary if block.
try this code
<?php
if(isset($_GET['page'])){
if($_GET['page']=="gallery"){
include "template/gallery.php";
}
}
else{
include "template/home.php";
}
?>
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"
very new to this, so I apologize in advance for rookie mistakes.
I am currently working through one of the PHP/MySQL introductory series from lynda.com. I am loving it thus far, and have been having success, but this one's got me confused.
I have created a form for user input, called new_subject.php. The following is called create_subject.php, and it meant to process the form data from new_subject.php. Please keep in mind that I have not yet added any code to do this, I am just testing a redirect function:
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<?php
if (isset($connection)) {mysqli_close($connection);}
?>
The point of the redirect_to function is to redirect the user to the form at new_subject.php if they were to type in create_subject.php manually in the browser. Here is what redirect_to function looks like in functions.php:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
I am getting the following error when trying to get to create_subject.php manually:
Fatal error: Call to undefined function redirect_to()
The tutorial video says that I can either turn on output buffering, which I have tried to do in my php.ini file, but the error remains the same. The video says I can do that, or "fix the white space issue." I have no idea what this means.
I would appreciate any info on what this "white space issue" is, and if there is anything else I can do here. As far as I can see, I should be able to call this function without issues.
Thanks
for the whitespace issue, there must be no output prior to calling location header, in your code.. there must be no whitespace outside the php tags..
just rewrite so that all those are enclose in a single php tag and make sure there is no space outside of that, even a single one.
<?php
require_once("../includes/db_connection.php");
require_once("../includes/functions.php");
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
if (isset($connection)) {mysqli_close($connection);}
?>
read here.. How to fix "Headers already sent" error in PHP
however, your main issue is the function is not found..
check your functions.php and make sure redirect_to is define properly. if that is the case, then most likely issue is in your "require_once" must be pointing to a non existent file. can you tell us your folder structure?
your functions.php must be located in a folder called includes located outside the folder where your create_subject.php is located.
if the 'includes' folder is located inside the folder where create_subject.php is, then change your require_once to
require_once("includes/db_connection.php");
require_once("includes/functions.php");
if both are located in the same folder, then change your require_once to
require_once("db_connection.php");
require_once("functions.php");
This is my first time using CMSMS and after researching, I found I had to use the User Defined Tags (UDTs) to use custom php. I did that successfully and all my code worked. I wanted to include a php file for mobile detection, but when I do and refresh the page, it's just a completely blank page.
When I view the source, it's entirely empty. I have an if statement after the include and narrowed it down to the include line being the problem. It's very simple:
include '/Mobile-Detect.php';
$detect = new Mobile_Detect;
// any mobile device (phones or tablets)
if($detect->isMobile()){
if(!$_COOKIE['ppc'] == 'true'){
header('Location: /mobile');
}else{
header('Location: /mobile?ppc=true');
}
}
So I'm not sure why other blocks of code I have in other UDTs works and the include doesn't even throw an error. Hopefully a more experienced CMSMSer can help me out on this.
Thank you
I got around it by editing the include.php file at the root level. I first had to check if it was an admin page or not since the file is included in the front and back end. Then ran my code to check for mobile devices.
End result:
if(!isset($CMS_ADMIN_PAGE)){
// code to be executed on non-admin pages
}
Note:
I also tried javascript, but in the UDT it was expecting php, so I added it to the template and then it was trying to render the curly braces in my if statement as smarty tags. So annoying.
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.