/    Sign up×
Community /Pin to ProfileBookmark

check if the page exists

I’m using this code to set a variable wich tell the script what file to load:

[code=php]
if(isset($_GET[“page”])) {
$file = $_GET[“page”];
$file=$page.’.txt’;
} else {
$file = ‘error.txt’;
}
[/code]

this is set like this:

[url]www.mydomainname.com/script.php?page=file[/url]

this loads file.txt

I have it working so if there is no variable set in the URL it loads the error.txt but I need to have the script check if the page exists incase there is a spelling error in the URL

e.g.

[url]www.mydomainname.com/script.php?page=fille[/url]

I need the script to then load error.txt

I’ve got this code but i’m unsure how to implement it.

[code=php]
if (file_exists($file)) {

do this
or die(
do this
); }
[/code]

Cheers
Chris

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms][url=http://www.devshed.com/c/a/PHP/PHP-Security-Mistakes/]Be careful...[/url][/font]
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 11.2004 — this didn't work as I get a Parse error.

but am I on the right track....

I replaced:
[code=php]if(isset($_GET["page"])) {
$file = $_GET["page"];
$file=$page.'.txt';
} else {
$file = 'error.txt';
}[/code]


with:

[code=php]$pages = array('page1', 'page2', 'page3', 'page4');
if( in_array($page, $pages) )
{
$file = $page.'.txt';
{
else
{
$file = 'error.txt';
} [/code]


like i said it didn't work.

any ideas or advice.

Chris
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]You'll have to pre-set the variable [/font][font=courier new]$page[/font][font=trebuchet ms] as [/font][font=courier new]$_GET["page"][/font][font=trebuchet ms].[/font]

[code=php]
$page = @$_GET["page"];
$pages = array("page1","page2","page3");

if(in_array($page, $pages)
and file_exists($page.".txt")){
$file = $page.".txt";
} else {
$file = "error.txt";
}

readfile($file);
[/code]
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 11.2004 — Ok I did that but I still get the Parse Error on line 8 which is the "else"

here is what I have done:
[code=php]$_GET["page"];
$pages = array('page1', 'page2', 'page3', 'page4');
if( in_array($page, $pages) )
{
$file = $page.'.txt';
{
else
{
$file = 'error.txt';
} [/code]


is this correct and is this what you meant for me to do when you said be careful?

Cheers

Chris
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]I edited my last post. Try the code I posted.[/font]
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 11.2004 — ok I only just got that.....I'll try that now
Copy linkTweet thisAlerts:
@cybercampbellauthorSep 11.2004 — Cool, all works!!

Thanks,

Chris ?
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]Happy to help. ? [/font]
Copy linkTweet thisAlerts:
@Skid94Oct 01.2006 — I have a server I run from home for my php since my website host doesnt. My dilemma is that I dont run the server during the week and my website iframes show server down when trying to load the php files. How can I ask "if" the server is running and load normally and "if" it is offline, load a default offline image. Remember my website host doesnt support php so I cant even use that.

Thank you for any help.

Scott P
×

Success!

Help @cybercampbell 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.5,
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,
)...