• 0

PHP Redirect based on Day


Question

Hey,

So I don't know a thing about PHP and I'm trying to get a page to redirect to another page based on what day it is. I found the code below that sounded like it was going to be perfect for what I needed, except it doesn't appear to work, it always uses the last line of code for the redirect regardless of day. Could you take a look at this and help me figure out what's wrong? I spent last night trying to figure this out myself, but just no luck (like I said, I don't know anything about PHP).

This is the code I was using:

<?php
 date_default_timezone_set('America/Los_Angeles');
 $b = time () ; 
 $day = date("D",$b) ; 

 //Here we redirect the user to a different programming site based on what day of the week it is. 

 switch($day){ 
 case "Sun": header( 'Location: http://php.about.com' ) ; 
 case "Mon": header( 'Location: http://webdesign.about.com' ) ; 
 case "Tue": header( 'Location: http://javascript.about.com' ) ; 
 case "Wed": header( 'Location: http://perl.about.com' ) ; 
 case "Thu": header( 'Location: http://python.about.com' ) ; 
 case "Fri": header( 'Location: http://ruby.about.com' ) ; 
 case "Sat": header( 'Location: http://cplus.about.com' ) ; 
 } 

 ?> 

Thank you all so much!

Dean

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

There's nothing wrong with that code.

Keep in mind though that you have to place it at the top of your document. It works by sending out header information, which AFAIK can only be done when you place your code before any HTML is processed.

edit: the error you're probably getting is

Warning: Cannot modify header information

Link to comment
Share on other sites

  • 0

With case statements, if you don't add a break, it will fall through to the next statement when it reaches the end of the current one.

<?php
 date_default_timezone_set('America/Los_Angeles');
 $b = time () ; 
 $day = date("D",$b) ; 

 //Here we redirect the user to a different programming site based on what day of the week it is. 

 switch($day){ 
 case "Sun": header( 'Location: http://php.about.com' ) ; break;
 case "Mon": header( 'Location: http://webdesign.about.com' ) ; break;
 case "Tue": header( 'Location: http://javascript.about.com' ) ; break;
 case "Wed": header( 'Location: http://perl.about.com' ) ; break;
 case "Thu": header( 'Location: http://python.about.com' ) ; break;
 case "Fri": header( 'Location: http://ruby.about.com' ) ; break;
 case "Sat": header( 'Location: http://cplus.about.com' ) ; break;
 } 

 ?> 

Link to comment
Share on other sites

  • 0

It is not working as expected because you are missing break line. Here is an example.

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

oooops late by seconds... :p

Link to comment
Share on other sites

  • 0

With case statements, if you don't add a break, it will fall through to the next statement when it reaches the end of the current one.

<?php
 date_default_timezone_set('America/Los_Angeles');
 $b = time () ; 
 $day = date("D",$b) ; 

 //Here we redirect the user to a different programming site based on what day of the week it is. 

 switch($day){ 
 case "Sun": header( 'Location: http://php.about.com' ) ; break;
 case "Mon": header( 'Location: http://webdesign.about.com' ) ; break;
 case "Tue": header( 'Location: http://javascript.about.com' ) ; break;
 case "Wed": header( 'Location: http://perl.about.com' ) ; break;
 case "Thu": header( 'Location: http://python.about.com' ) ; break;
 case "Fri": header( 'Location: http://ruby.about.com' ) ; break;
 case "Sat": header( 'Location: http://cplus.about.com' ) ; break;
 } 

 ?> 

Oh crap. Totally missed that! I checked it out real quick and it was giving me the redirect, so I supposed it worked. That's what you get when you rush things. :laugh:

Link to comment
Share on other sites

  • 0

Don't forget to add an 'exit' call after the header function. :)

<?php
$map = array(
 'Sun' => 'http://php.about.com',
 'Mon' => 'http://webdesign.about.com',
 /* ...the rest... */
 'Sat' => 'http://cplus.about.com'
);

date_default_timezone_set('America/Los_Angeles');

header(
 sprintf(
	'Location: %s',
	$map[date('D')]
 )
);

exit;
?>

Link to comment
Share on other sites

  • 0

I hate redundancy in code, hate it!

This whole bit, which would now be even longer since you added breaks...

 $b = time () ; 
 $day = date("D",$b) ; 

 //Here we redirect the user to a different programming site based on what day of the week it is. 

 switch($day){ 
 case "Sun": header( 'Location: http://php.about.com' ) ; 
 case "Mon": header( 'Location: http://webdesign.about.com' ) ; 
 case "Tue": header( 'Location: http://javascript.about.com' ) ; 
 case "Wed": header( 'Location: http://perl.about.com' ) ; 
 case "Thu": header( 'Location: http://python.about.com' ) ; 
 case "Fri": header( 'Location: http://ruby.about.com' ) ; 
 case "Sat": header( 'Location: http://cplus.about.com' ) ; 
 } 

...could be done with two little lines and no conditionals!

$days = array('php', 'webdesign', 'javascript', 'perl', 'python', 'ruby', 'cplus');
header('Location: http://' . $days[date('w')] . '.about.com');

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.