Broken query string in PHP

Reply

Join Date: Nov 2008
Posts: 66
Reputation: whitestream6 is an unknown quantity at this point 
Solved Threads: 0
whitestream6 whitestream6 is offline Offline
Junior Poster in Training

Broken query string in PHP

 
0
  #1
Nov 2nd, 2009
This is my code which is intended to produce an URL like the following:
www.mysite.com/test.php?day=1

  1. <?
  2. if (!$_GET['day']) {
  3. $day = date('N');
  4. }
  5. elseif ($_GET['day']==1){ ?>
  6. <?php include("test1.php") ?>
  7. <? }else if($_GET['day']==2){ ?>
  8. <?php include("test2.php") ?>
  9. <? }else if($_GET['day']==3){ ?>
  10. <?php include("test3.php") ?>
  11. <? }else if($_GET['day']==4){ ?>
  12. <?php include("test4.php") ?>
  13. <? }else if($_GET['day']==5){ ?>
  14. <?php include("test5.php") ?>
  15. <? }else if($_GET['day']==6){ ?>
  16. <?php include("test6.php") ?>
  17. <? }else if($_GET['day']==5){ ?>
  18. <?php include("test7.php") ?>
  19. <? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
  20. invalid id (or page not found)
  21. <? } ?>
  22. ?>
  23. <?php
  24. $dat=date("w");
  25. switch ($dat)
  26. {
  27. case 1:
  28. include("test1.php");
  29. break;
  30. case 2:
  31. include("test2.php");
  32. break;
  33. case 3:
  34. include("test2.php");
  35. break;
  36. case 4:
  37. include("test4.php");
  38. break;
  39. case 5:
  40. include("test5.php");
  41. break;
  42. case 6:
  43. include("test6.php");
  44. break;
  45. }
  46. ?>

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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 66
Reputation: whitestream6 is an unknown quantity at this point 
Solved Threads: 0
whitestream6 whitestream6 is offline Offline
Junior Poster in Training
 
0
  #2
Nov 2nd, 2009
Basically what I'm trying to do... get the script to display a different page every day of the week.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,071
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-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).

  1. <?php
  2. //if using 1 to 7 - the 'N' format
  3.  
  4. if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  5. $dat = $_GET['day'];
  6. }else{
  7. $dat = date('N');
  8. }
  9. switch ($dat)
  10. {
  11. case 1:
  12. include("test1.php");
  13. break;
  14. case 2:
  15. include("test2.php");
  16. break;
  17. case 3:
  18. include("test2.php");
  19. break;
  20. case 4:
  21. include("test4.php");
  22. break;
  23. case 5:
  24. include("test5.php");
  25. break;
  26. case 6:
  27. include("test6.php");
  28. break;
  29. case 7:
  30. include("test7.php");
  31. break;
  32. }
  33. ?>
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 455
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
1
  #4
Nov 3rd, 2009
You could even simplify it further:
  1. <?php
  2. $dat = intval(@$_GET['day']);
  3. if($dat < 1 || $dat > 7) {
  4. $dat = date('N');
  5. }
  6. include("test{$dat}.html");
  7. ?>
The 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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 66
Reputation: whitestream6 is an unknown quantity at this point 
Solved Threads: 0
whitestream6 whitestream6 is offline Offline
Junior Poster in Training
 
0
  #5
Nov 3rd, 2009
Originally Posted by ardav View Post
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).
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,071
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #6
Nov 3rd, 2009
Originally Posted by whitestream6 View Post
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
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.

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:

  1. if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  2. $dat = $_GET['day'];
  3. echo "Good querystring: " . $dat;
  4. }else{
  5. $dat = date('N');
  6. echo "Bad or no querystring, using today: " . $dat;
  7. }

Also, "case3:" later on refers to page2.php - should be page3.php.
Last edited by ardav; Nov 3rd, 2009 at 3:27 pm.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 66
Reputation: whitestream6 is an unknown quantity at this point 
Solved Threads: 0
whitestream6 whitestream6 is offline Offline
Junior Poster in Training
 
0
  #7
Nov 3rd, 2009
Originally Posted by ardav View Post
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.
I tried atli's suggestion, and it partly worked.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,071
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #8
Nov 3rd, 2009
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.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC