/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Creating a "Header" redirect depending on the month

[i](Hmmm…that’s weird: how did my reply end up before the original post?)[/i]

I might do that part as:

[code=php]
$month = intval($month);
if($month < 1 or $month > 12) {
// display/log error or whatever you want to do in this case
}
else {
$monthName = array(1 => ‘january’, ‘february’, ‘march’, ‘april’, ‘may’,
‘june’, ‘july’, ‘august’, ‘september’, ‘october’, ‘novemeber’, ‘december’);
$location = sprintf(“http://example.com/&#37;02d-%s.php”, $month, $monthName[$month]);
if(!headers_sent()) {
header(“Location: $location”);
exit;
}
else {
// display/log error or whatever you want to do in this case
}
}
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@beylahDec 17.2010 — This is not working, can someone please help - thanks.

[code=php]if($_POST['Submit']){

$id=$_POST['id'];
$month=$_POST['month'];
$day=$_POST['day'];
$event=$_POST['event'];
$notes=$_POST['notes'];

mysql_query("UPDATE bnr SET month='$month', day='$day', event='$event', notes='$notes' WHERE id='$id'");

if ($month=="01"){$location="01-january.php";}
if ($month=="02"){$location="02-february.php";}
if ($month=="03"){$location="03-march.php";}
if ($month=="04"){$location="04-april.php";}
if ($month=="05"){$location="05-may.php";}
if ($month=="06"){$location="06-june.php";}
if ($month=="07"){$location="07-july.php";}
if ($month=="08"){$location="08-august.php";}
if ($month=="09"){$location="09-september.php";}
if ($month=="10"){$location="10-october.php";}
if ($month=="11"){$location="11-november.php";}
if ($month=="12"){$location="12-december.php";}

header("Location: $location");
exit;
}
[/code]
Copy linkTweet thisAlerts:
@beylahDec 17.2010 — Thank you for the code but I do not understand it - is there anyway you can explain it to me?
Copy linkTweet thisAlerts:
@Jarrod1937Dec 17.2010 — 
[code=php]
// Make sure $month is an int //
$month = intval($month);

// Test if $month is not a valid value, aka is not >= 1 and <=12
// If not valid throw an error message, if valid continue //
if($month < 1 or $month > 12) {
// display/log error or whatever you want to do in this case
}else {

// Create an array starting with an index of 1 so that it can be referenced
// by the variable $month, which starts at 1 //
$monthName = array(1 => 'january', 'february', 'march', 'april', 'may',
'june', 'july', 'august', 'september', 'october', 'novemeber', 'december');

// Use sprintf to format the redirect location string
// the first variable placeholder in sprintf is replaced by the value of month
// the second is replaced by the month name referenced by $month as the
// index value //
$location = sprintf("http://example.com/%02d-%s.php", $month, $monthName[$month]);

// Make sure no headers have already been sent, if so you can't send the
// redirect header
if(!headers_sent()) {

// Send the redirect header with the location string generated above //
header("Location: $location");
exit;
}
else {
// display/log error or whatever you want to do in this case
}
}
[/code]
[/QUOTE]


I've commented on the quoted code.
Copy linkTweet thisAlerts:
@NogDogauthorDec 17.2010 — Couldn't have said it better myself. The main points I wanted to get across were to (a) never assume data is correct and within acceptable bounds, and (b) do some sort of error-reporting or logging to tell you what went wrong when it does. Or in just two words: defensive coding. ? (A third and less important point was the idea of using the array to get the month instead of 12 if conditions.)
Copy linkTweet thisAlerts:
@ehimeDec 18.2010 — Looks like a indexing jump Nog?
Copy linkTweet thisAlerts:
@beylahDec 20.2010 — thank you so much for the explanation - it really helped and i fully understand it
×

Success!

Help @NogDog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...