| | |
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: 54
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 27 Days Ago
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; } ?>
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
1
#4 27 Days Ago
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: 54
Reputation:
Solved Threads: 0
0
#5 27 Days Ago
•
•
•
•
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 27 Days Ago
•
•
•
•
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; 27 Days Ago at 3:27 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: Nov 2008
Posts: 54
Reputation:
Solved Threads: 0
0
#7 26 Days Ago
•
•
•
•
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.
-1
#8 26 Days Ago
Sounds like a stupid question, but are the contents of the different files actually different? Try the 'echo' code in my previous post to see which day is being passed.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
![]() |
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
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error errors execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javascript joomla keywords limit link login loop mail menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube





