I have 3 template pages (smarty templates
) and 1 PHP file with conditions that show different data depending of the opened page.
directory smarty
page1.tpl
page2.tpl
page3.tpl
directory public_html
file.php
Here is the file.php
if( stripos($_SERVER['REQUEST_URI'], 'page1.php' )) {
echo 'page1';
} else if( stripos($_SERVER['REQUEST_URI'], 'page2.php' )) {
echo 'page2';
} else if( stripos($_SERVER['REQUEST_URI'], 'page3.php' )) {
echo 'page3';
}
Now when I open https://example.com/page1.php
should go in first condition and echo me page1. This doesn't happen. It is showing me blank page.
I guess this is happening because I don't actually have file called page1.php
. If I create even empty page1.php
file all is working but the idea is to have all in one page file.php
and show different data based on opened page.
What I'm trying to do in file.php
is to get the url and if ends page1.php
show data for page1 and so on.
Is this how should be done or there is another way?
CodePudding user response:
Have you redirected the requests from page1.php to file.php (e.g. on Apache via mod_rewrite)? Otherwise nothing would be executed when the file page1.php isn't present.
Maybe pointing to example.com/file.php?p=page1.php works. But in this case it would be better to check the get parameters in your script. For example:
if (isset($_GET['p']) { $page = $_GET['p']; }
if ($page == 'page1.php') ...
For the latter you could consider to use a switch
statement.