0

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?

6
  • 2
    On a side note, you can't have two elses for one if. Commented Nov 22, 2013 at 20:35
  • 1
    Would your href not need to be home.php?page=my_page_x? Commented Nov 22, 2013 at 20:36
  • Probably be easier to make use of case, but that's just me. Commented Nov 22, 2013 at 20:37
  • @MikeBrant I don't think home.php is required when the link is called from home.php(though it is good practice). I could be mistaken, though. Commented Nov 22, 2013 at 20:45
  • @Mike Brant, when I do click on those links my new link is "pathToFolder/home.php?page=my_page_x" Commented Nov 22, 2013 at 20:48

3 Answers 3

1

Have you thought about using

header("Location: $filename");

That will reload the page on the new url.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I could just set my href's as href="my_page_x.php" and it will work, but my aim is to understand why does this code doesn't work when it should (it's simplified example from one of the youtube tutorials)
0

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.

1 Comment

well, my server is my own PC with windows on it -_- btw, thx for GET tip
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.