| | |
Broken query string in PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 0
This is my code which is intended to produce an URL like the following:
www.mysite.com/test.php?day=1
The query string does work, but shows the same page - test1.php, no matter what day I call up in the PHP URL.
Anyone managed to get this working, as I tried and tried but can't figure it out!
www.mysite.com/test.php?day=1
PHP Syntax (Toggle Plain Text)
<? if (!$_GET['day']) { $day = date('N'); } elseif ($_GET['day']==1){ ?> <?php include("test1.php") ?> <? }else if($_GET['day']==2){ ?> <?php include("test2.php") ?> <? }else if($_GET['day']==3){ ?> <?php include("test3.php") ?> <? }else if($_GET['day']==4){ ?> <?php include("test4.php") ?> <? }else if($_GET['day']==5){ ?> <?php include("test5.php") ?> <? }else if($_GET['day']==6){ ?> <?php include("test6.php") ?> <? }else if($_GET['day']==5){ ?> <?php include("test7.php") ?> <? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?> invalid id (or page not found) <? } ?> ?> <?php $dat=date("w"); switch ($dat) { case 1: include("test1.php"); break; case 2: include("test2.php"); break; case 3: include("test2.php"); break; case 4: include("test4.php"); break; case 5: include("test5.php"); break; case 6: include("test6.php"); break; } ?>
The query string does work, but shows the same page - test1.php, no matter what day I call up in the PHP URL.
Anyone managed to get this working, as I tried and tried but can't figure it out!
-1
#3 Nov 2nd, 2009
The querystring handling looks like just a shocking mess (sorry!). You've got short tags all over the place, multiple if/elseifs.
I really don't understand why you're using 'N' and 'w' - surely you need consistency?
You need to check for a $_GET['day'] and ensure that it's between 1 and 7 if using 'N' (Mon-Sun) or between 0 and 6 if using 'w' (Sun-Mon).
I really don't understand why you're using 'N' and 'w' - surely you need consistency?
You need to check for a $_GET['day'] and ensure that it's between 1 and 7 if using 'N' (Mon-Sun) or between 0 and 6 if using 'w' (Sun-Mon).
PHP Syntax (Toggle Plain Text)
<?php //if using 1 to 7 - the 'N' format if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){ $dat = $_GET['day']; }else{ $dat = date('N'); } switch ($dat) { case 1: include("test1.php"); break; case 2: include("test2.php"); break; case 3: include("test2.php"); break; case 4: include("test4.php"); break; case 5: include("test5.php"); break; case 6: include("test6.php"); break; case 7: include("test7.php"); break; } ?>
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
1
#4 Nov 3rd, 2009
You could even simplify it further:
The
PHP Syntax (Toggle Plain Text)
<?php $dat = intval(@$_GET['day']); if($dat < 1 || $dat > 7) { $dat = date('N'); } include("test{$dat}.html"); ?>
intval function always returns a number, or 0 if the input is invalid, which would be the case if no "day" value was passed. Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 0
0
#5 Nov 3rd, 2009
•
•
•
•
The querystring handling looks like just a shocking mess (sorry!). You've got short tags all over the place, multiple if/elseifs.
I really don't understand why you're using 'N' and 'w' - surely you need consistency?
You need to check for a $_GET['day'] and ensure that it's between 1 and 7 if using 'N' (Mon-Sun) or between 0 and 6 if using 'w' (Sun-Mon).
How would I fix this? This code is for my localhost radio station site (virtualhost name on Apache www.mylocalhostradiosite.co.uk)
and the URLs are meant to be:
http://www.mylocalhostradiosite.co.u...dule.php?day=3
(with the content changing per day).
Thanks for your help so far
-1
#6 Nov 3rd, 2009
•
•
•
•
Your code almost worked... except it displayed the same page repeatedly, despite the contents of 1, 2, 3, 4, 5, 6 and 7.php all being different.
How would I fix this? This code is for my localhost radio station site (virtualhost name on Apache www.mylocalhostradiosite.co.uk)
and the URLs are meant to be:
http://www.mylocalhostradiosite.co.u...dule.php?day=3
(with the content changing per day).
Thanks for your help so far
BTW: Atli's solution is much nicer than mine - I attempted to fix your code, whereas he's done a complete makeover. His code works as long as you synchronize the 'day number' with the include file name. However, if you were to have different filenames for certain days or even the same filenames for certain days (e.g. Sat/Sun both using the same file), the code wouldn't work 'as is'.
With regard to forcing an integer value on any $_GET value, this is useful if you don't care about assigning a default day or applying a day if the value is 'close'. This should be fine if using the date('N') - Atli points out, valid days = 1 to 7, therefore a totally off the wall value will return a '0' - which can be used to throw an error. However, if you go with the 0 to 6 (date('w')), which I have to admit to favouring, you will find Sunday as the default page for incorrect querystrings.
Anyway, enough rambling. I'd give Atli's solution a go as it's much prettier.

//EDIT
If you want to see what's going on, try this:
PHP Syntax (Toggle Plain Text)
if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){ $dat = $_GET['day']; echo "Good querystring: " . $dat; }else{ $dat = date('N'); echo "Bad or no querystring, using today: " . $dat; }
Also, "case3:" later on refers to page2.php - should be page3.php.
Last edited by ardav; Nov 3rd, 2009 at 3:27 pm.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 0
0
#7 Nov 3rd, 2009
•
•
•
•
Your links don't work.
BTW: Atli's solution is much nicer than mine - I attempted to fix your code, whereas he's done a complete makeover. His code works as long as you synchronize the 'day number' with the include file name. However, if you were to have different filenames for certain days or even the same filenames for certain days (e.g. Sat/Sun both using the same file), the code wouldn't work 'as is'.
With regard to forcing an integer value on any $_GET value, this is useful if you don't care about assigning a default day or applying a day if the value is 'close'. This should be fine if using the date('N') - Atli points out, valid days = 1 to 7, therefore a totally off the wall value will return a '0' - which can be used to throw an error. However, if you go with the 0 to 6 (date('w')), which I have to admit to favouring, you will find Sunday as the default page for incorrect querystrings.
Also, "case3:" later on refers to page2.php - should be page3.php.
As for the URLs not being able to be found, they're only accessible locally, mapped via virtualhosts in Apache and a HOSTS file - otherwise non-existent on the Internet.
![]() |
Similar Threads
- Query string parameters in PHP (PHP)
- Query string in URLs (PHP)
- How to hide query string in url with .htaccess (PHP)
- Help! I cannot login with Webrequest.Create with query string (C#)
- URL Rewriting pass query string (Linux Servers and Apache)
- Dead query string urls to new extensionless urls (Linux Servers and Apache)
- Dynamic query string (Search Engine Optimization)
Other Threads in the PHP Forum
- Previous Thread: How To Print Directly to Printer
- Next Thread: save file xls or txt
Views: 339 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube






