Anton
Anton

Reputation: 35

php + href redirection

I have next code in my home.php file:

<html>    
    <body>
    <table border="3">
        <tr><td>
            <a href="?page=my_page_1">Page 1</a>
        </td></tr>

        <tr><td>
            <a href="?page=my_page_2">Page 2</a>
        </td></tr>

    </table>

    <?php
        if(isset($_GET['page'])){
            $page = $_GET['page'];
            $filename = $page.'.php';

            if(file_exists($filename)){
                include($filename);
            }
            else{
                echo("File does not exist");
            }
        }
        else{
            echo("Page does not set");
        }
    ?>
    </body>    
</html>

I'am expecting to page being reloaded as a certain .php file when one of the links is getting pressed. But, by some reason, nothing happens instead. Files "my_page_1.php" and "my_page_2.php" does exist and are located in the same folder with "home.php". I guess it doesn't work as I think it should, so could someone explain me what is really happening when links are getting pressed?

Upvotes: 0

Views: 95

Answers (3)

Anton
Anton

Reputation: 35

Seems like php would be executed only on and by the server and not as a simple script by your browser.

Upvotes: 0

Asta
Asta

Reputation: 1579

You need to have your server configured in order to allow links that start with ?page. Otherwise you will need to add the current url as suggested in the comments.

You can checkout Apache mod_rewrite for more details. A better approach in using mod_rewrite would be to keep the page name but remove the extension i.e home?page=my_page_x. This is more flexible and allows search friendly and more readable urls.

Note: In production you shouldn't include a file from a GET variable like that as it opens up a lot of security issues.

Upvotes: 0

Thornuko
Thornuko

Reputation: 287

Have you thought about using

header("Location: $filename");

That will reload the page on the new url.

Upvotes: 1

Related Questions