Home > Enterprise >  Perform different actions depending of the opened template
Perform different actions depending of the opened template

Time:03-30

I have 3 template pages (smarty templates) and 1 PHP file with conditions that show different data depending of the opened page.

directory smarty

  1. page1.tpl
  2. page2.tpl
  3. page3.tpl

directory public_html

  1. 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.

  • Related