/    Sign up×
Community /Pin to ProfileBookmark

PHP includes & requires

Hey, I’m quite new to PHP, and I’m only using it for includes & getting current date and that kinda stuff. I prefer to use HTML. It’s easier ?

Anyway, on some sites, you see something like

[url]www.site.com/index.php?page=3[/url]

and such, where you don’t actually have to change the page you’re viewing; only which file is included at a given point. Could anyone explain how this works and how to do it for me? Thank you.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaMay 26.2004 — www.site.com/index.php?page=3

page=3 is called query string

where page is the name and 3 is the value
Copy linkTweet thisAlerts:
@AdamGundryMay 26.2004 — The basic code would look something roughly like this:

[code=php]require $_GET['page'] . '.txt';[/code]

Then loading (for example) index.php?page=3 would contain the content of the file '3.txt'. Be aware there can be _major_ security issues with this if you don't validate the passed value properly - for example, a cracker could gain access to hidden files on your server.

Adam
Copy linkTweet thisAlerts:
@LotusauthorMay 28.2004 — Thanx dude, think I get it now... But one more thing, how do I change this value dynamically when e.g. the user clicks a link in a menu? You know, if I only want to change one specific include?
Copy linkTweet thisAlerts:
@AdamGundryMay 28.2004 — You simply link to the current page, with a different query string. For example, you could link to "index.php?page=2" and get different content after the page refreshes. To change the content without a refresh is more difficult and requires either frames or JavaScript, neither of which are good solutions.

Adam
×

Success!

Help @Lotus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...