?
Solved

Need a PHP Redirect Script with multiple conditions

Posted on 2006-05-20
10
Medium Priority
?
462 Views
Last Modified: 2010-08-05
I am needing a script for a school homework help site.  I'm good at working with existing PHP, but I can't find very many references to do what I need for this one.

I need a PHP redirect script that triggers on one of multiple conditions, as follows, in order of precedence:

1) Day of the year (for special days, such as holidays)
  a) For Christmas, which will cause the site to redirect between 12/24 and 01/02 of each year.
  b) For Thanksgiving (we can modify it to correct date every year).
  c) For July 4
  d) Generic single day so we can add other days of the year as desired.
  e) Generic single week so we can add other breaks (like for Spring Break).
2) Day of the week (for the weekend, especially Sunday)
  a) Weekend hours differ from weekday hours, since kids are out of school, so the hours that follow in section 3 will vary, however I need the script set up so that I can modify it for the corrected times.
3) Hour of the day (for redirection to different pages for after hours operations).
  a) Between 2200 hours and 1400 hours the following day (after hours)
  b) Between 1400 hours and 2200 hours (hours of operation)

This script will be the index page for the directory, and the page that is loaded will depend on the condition met above.  Please just list main.php as the page that loads for each of these conditions as the redirect location, and I'll modify it as needed.

I hope you guys will understand what I'm looking for.  If not, I'll happily give more details.

Thanks.  :)
0
Comment
Question by:CapTech
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16726859
what  is the periority of doing rediretion ??
i mean if it is 1200 hour on weekend, is it the same as 1200 hour on friday ?
how should redirection start, by week ends, then special days then time ? or time then week ends then special days !!

also, which php version you are using ?
0
 
LVL 2

Author Comment

by:CapTech
ID: 16726911
PHP version 5.  Let me break this down a bit more for you guys:

On holidays and during special breaks, like spring break, the site will redirect to pages that will announce the site as closed for the holiday or break.

As far as the actual hours, that part won't matter, because I can change the >=  or <= statements to the appropriate times if those are used.

The listed priority is in my original post, but I'll give hit here in a more simple version:

1) Holidays
2) Day of the Week
3) Hours of the Day

I've went with this format for priority, because if the day happens to be a holiday, the site will direct to the "we're closed" pages.  If it's not a holiday, then the day of the week will be conditioned.  The only days that need special conditioned are the weekend (Saturday and Sunday), the rest of the week can be the default response.  As a subpart of both of the days of the week condition (weekend and non-weekend) will need 2 separate time period redirects, one for 2200 - 1400 and one for 1400 - 2200 based on the time on the server, not the time on the client side.

In fact, to make things easier, the code presented here can just show those hour ranges for both the weekend and non-weekend portions of the code, and I will modify it to the actual hours the group decides to go with on the weekends.

Hope this helps.  :)
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16727500
2003: Thu, Nov 27
2004: Thu, Nov 25
2005: Thu, Nov 24
2006: Thu, Nov 23
2007: Thu, Nov 22
2008: Thu, Nov 27

$redirect1 = array();
$redirect1[] = array('Y/m/d', '2003/11/27', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2004/11/25', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2005/11/24', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2006/11/23', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2007/11/22', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2008/11/27', 'Thanksgiving.html');
$redirect1[] = array('m/d', '7/4', 'JulyFourth.html');
$redirect1[] = array('Y/m/d', '2006/12/05', 'Sinterklaas.html'); // Dutch
$redirect1[] = array('Y/W', '2006/12', 'SpringBreak.html') ; //12th week of 2006
$redirect1[] = array('w', '1', 'Sunday.html');
$redirect1[] = array('w', '6', 'Saturday.html');

Then you can use:
// Christmas
if(date('W') >= '52' || date('m/d') < '01/02') {
  header('Location: christmas.php');
  exit();
}
// Redirects based on php's date() function
foreach($redirect1 as $date) {
  if(date($date[0]) == $date[1]) {
      header('Location: '.$date[2]);
      exit();
  }
}
// Times a day:
if(date('H') < 14 || date('H') > 22) {
  header('Location: outofoffice.html');
  exit();
} else {
  header('Location: main.php');
  exit();
}

Hope this helps,

-r-
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:CapTech
ID: 16728858
This script does what I needed.  Thank you very much Roonaan!

As I said, I am a PHP debugger, so here are few minor errors noted in this code just for reference and for any Premium EE users that ever revisit this topic:

$redirect1[] = array('m/d', '7/4', 'JulyFourth.html'); <-- '7/4' should be '07/04'

$redirect1[] = array('w', '1', 'Sunday.html'); <-- 'w','1','Sunday.html' should be 'w','0','Sunday.html'

Here is the clean code used as my final resulting index.php page:

<?

$redirect1 = array();
$redirect1[] = array('Y/m/d', '2003/11/27', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2004/11/25', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2005/11/24', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2006/11/23', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2007/11/22', 'Thanksgiving.html');
$redirect1[] = array('Y/m/d', '2008/11/27', 'Thanksgiving.html');
$redirect1[] = array('m/d', '07/04', 'JulyFourth.html');
$redirect1[] = array('Y/m/d', '2006/12/05', 'Sinterklaas.html'); // Dutch
$redirect1[] = array('Y/W', '2006/12', 'SpringBreak.html') ; //12th week of 2006
$redirect1[] = array('w', '0', 'Sunday.html');
$redirect1[] = array('w', '6', 'Saturday.html');

// Begin redirects
// For Christmas
if(date('W') >= '52' || date('m/d') < '01/02') {
  header('Location: christmas.php');
  exit();
}

// Redirects based on the redirect array above
foreach($redirect1 as $date) {
  if(date($date[0]) == $date[1]) {
      header('Location: '.$date[2]);
      exit();
  }
}
// Redirects based on hour of the day
if(date('H') < 14 || date('H') > 22) {
  header('Location: afterhours.php');
  exit();
} else {
  header('Location: main.php');
  exit();
}

?>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16728873
Thanks for correcting me. I wrote the code from heart and didn't actually test it. You might also want to test the date('W' >= 52) I was not sure wether that should have been week 51.

-r-
0
 
LVL 2

Author Comment

by:CapTech
ID: 16729457
I believe you would be correct, that should be 51 since the array starts with the first week as 0.

Regards,

D
0
 

Expert Comment

by:adgcanada
ID: 23842880
This looks like exactly what I have been in search for! A few questions as I am not a coder but fluent with design/html/css and a very basic understanding of php.

The top portion of code with $redirect. are those various ways to list the dates? Or does all that code have to be in place? Can I remove or add another line for those correct? And there i put the date I want the redirect to take place?

Basically I want to redirect for holidays but I also need to redirect after hours when our office is closed.
I would want the after hours redirect to happen every night. For now at least.

0
 

Expert Comment

by:adgcanada
ID: 23842882
I am basically confused what $redirect1[] = array('w', '6', 'Saturday.html'); is ? I know what $redirect1[] = array('Y/m/d', '2003/11/27', 'Thanksgiving.html'); is as its reading from php date function right? So it reads the year, day and  month to redirect to that page.

But not sure what 'array(w,6...  is ?
0
 

Expert Comment

by:adgcanada
ID: 23843085
ok I think i figured this out. can somebody just confirm for me if I am on the right track.
I have edited some code below more to what I need I think.

So "array('Y/W', '2009/12', 'springBreak.php') "  shows a new page for that week correct? 12 is the weeks out of 2009? That would show that url for that whole week?

" array('w', '0', 'sunday.php'); " means the weekday of 0 if sunday is the start of the week?
Is that correct?

I would like to show a different page for Saturday and sunday evenings.

For christmas. I would like it to show a different page from the 24th till the 1st of January.
Does the code below do that?

<?

$redirect1 = array();
$redirect1[] = array('Y/m/d', '2009/04/12', 'easter.php');
$redirect1[] = array('Y/m/d', '2009/11/31', 'halloween.php');
$redirect1[] = array('Y/m/d', '2009/12/25', 'christmas.php');
$redirect1[] = array('m/d', '07/04', 'JulyFourth.html');
$redirect1[] = array('Y/W', '2009/12', 'springBreak.php') ; //12th week of 2006
$redirect1[] = array('w', '0', 'sunday.php');
$redirect1[] = array('w', '6', 'saturday.php');

// Begin redirects
// For Christmas
if(date('W') >= '52' || date('m/d') < '01/02') {
  header('Location: christmas.php');
  exit();
}

// Redirects based on the redirect array above
foreach($redirect1 as $date) {
  if(date($date[0]) == $date[1]) {
      header('Location: '.$date[2]);
      exit();
  }
}
// Redirects based on hour of the day
if(date('H') < 14 || date('H') > 22) {
  header('Location: afterhours.php');
  exit();
} else {
  header('Location: index.php');
  exit();
}

?>
0
 

Expert Comment

by:adgcanada
ID: 23853595
ok I figured it out. Thanks for the help!

Question. this part. Is that in 24hrs?
if(date('H') < 14 || date('H') > 22) {

How would I tell it from 12am till 7am i want a different page to show?

if(date('H') < 24 || date('H') > 07) {
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question