...

View Full Version : sessions and using header("Location")



mhunt
08-31-2007, 03:54 PM
I have a script where i set the session variable and then redirect to another page, on the next page the session variable is getting reset and i have no clue why. anyone have an idea, here is the relevent parts of the script:



session_start();
$ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "http://{$_SERVER['SERVER_NAME']}/";
$_SESSION['ref'] = $ref;
header("Location: contest.php");

and then on the next page i have this:


session_start();
if(isset($_SESSION['ref'])) {
$ref = $_SESSION['ref'];
}
print_r($_SESSION);

CFMaBiSmAd
08-31-2007, 04:13 PM
Edit: never mind what I originally wrote here.

However, check your web server log for errors and/or turn on full php error reporting. This will tell you if any errors are occurring that are preventing the session from working.

mhunt
08-31-2007, 04:23 PM
unfortunately the server has display_errors turned off and the people above me won't let me ask the server admin to turn them back on.

i tried using the thing below but it still shows nothing


ini_set('display_errors','On');
error_reporting(E_ERROR | E_WARNING | E_PARSE);

CFMaBiSmAd
08-31-2007, 05:07 PM
What does your print_r() show?

The two pieces of code you posted work correctly on my test system, so there is nothing about just those lines of code that are preventing this from working. That would mean something else in your code or on your server is.

Do you know for a fact that sessions work and it is just the current code that does not work?

mhunt
08-31-2007, 05:14 PM
the sessions work it is just changing the referrer in the session var to something else when i do the header call i'll paste the whole code for you
p.s this code was not written by me i have to adapt it from an older site to this new script, i know its ugly sorry


<?
ini_set('display_errors','On');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "http://{$_SERVER['SERVER_NAME']}/";
setcookie("ref", $ref, time()+3600);
print_r($_COOKIE);
include("header_footer.php");
class LeapYearChecker {

function isLeapYear($year){
if(($year % 400) == 0){
return true; //Is LeapYear
}else if(($year % 100) == 0){
return false; //Not a LeapYear
}else if(($year % 4) == 0){
return true; //Is LeapYear
}else{
return false; //Not LeapYear
}
}
}
have_header();
$currentday = date('d');
$currentmonth = date('m');
$currentyear = date('Y');

?>
<?
if(!$_POST["submit"]) {
?>
<form action="<? echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
<label for="day">D:</label>
<input type="text" name="day" value="" size="2" />
<label for="month">M:</label>
<input type="text" name="month" value="" size="2" />
<label for="year">Y:</label>
<input type="text" name="year" value="" size="4" />
<input type="submit" name="submit" value="submit" />
</form>
<p>Please submit the day, month, and year you were born, to access the UK Rum Site.</p>
<?
} else {
$error_list = array();

if($_POST["day"] <= "2") {
if($_POST["day"] !== "" OR $_POST["day"] >= "1") {
}else{
$error_list[] = "<p>The day you were born should be entered as 2 numbers</p>";
}
}else{
//all is good
//echo "<p>day is 2 or less numbers</p>";
}
if($_POST["month"] <= "2") {
if($_POST["day"] !== "" OR $_POST["day"] >= "1"){
//echo "<p>This is fine</p>";
}else{
$error_list[] = "<p>The month you were born should be entered as 2 numbers</p>";
}
} else {
//echo "<p>month is 2 or less numbers</p>";
}

if($_POST["year"] <= "4") {
if($_POST["day"] !== "" OR $_POST["day"] >= "1"){
//echo "<p>This is fine</p>";
}else{
$error_list[] = "<p>The year you were born should be entered as 4 numbers</p>";
//echo "<p>why is the year not 4 numbers?</p>";
}
} else {
//echo "<p>the year is 4 characters long, GOOD.</p>";
}

if(ctype_digit($_POST["day"]) AND ctype_digit($_POST["month"]) AND ctype_digit($_POST["year"])) {
//echo "<p>All numbers, GOOD.</p>";
} else {
$error_list[] = "<p>Please enter your date of birth in numeric format.</p>";
}

///month day year verification of actual date
$thirtyone = array(01,1,03,3,05,5,07,7,08,8,10,12);
//print_r($thirtyone);

$thirty = array(04,4,06,6,09,9,11);
//print_r($thirty);

$leaper = array(02,2);
//print_r($leaper);

if (in_array($_POST["month"], $thirtyone) AND $_POST) {
// echo "<p>".$_POST["month"]." has 31 days</p>";
if($_POST["day"] <= "31"){
}else{
$error_list[] = "<p>There isn't even that many days in that month.</p>";
}
}

if (in_array($_POST["month"], $thirty) AND $_POST) {
//echo "<p>".$_POST["month"]." has 30 days<br />";
if($_POST["day"] <= "30"){
}else{
$error_list[] = "<p>There isn't even that many days in that month.</p>";
}
}

if (in_array($_POST["month"], $leaper) AND $_POST) {

$LYC = new LeapYearChecker();
$LYC->isLeapYear($_POST["year"]) == true ? $leapwhat="yes" :$leapwhat="no";

if($leapwhat == "yes"){
//echo "<p>".$_POST["month"]." has 29 days";
if($_POST["day"] <= "29"){
}else{
$error_list[] = "<p>There isn't even that many days in that month.</p>";
}
}else{
//echo "<p>".$_POST["month"]." has 28 days";
if($_POST["day"] <= "29"){
}else{
$error_list[] = "<p>I know it's a leap year, but there isn't even that many days in february, ever.</p>";
}
}
}

$submit_date = $_POST["year"]."-".$_POST["month"]."-".$_POST["day"];

$current_date = $currentyear."-".$currentmonth."-".$currentday;

$ageinseconds = strtotime($current_date) - strtotime($submit_date);

//divide by seconds in a year to get he exact age
//1 year = 31 556 926 seconds
$oneyear = "31556926";

$create_age = $ageinseconds / $oneyear;
$agearr = explode(".", $create_age);

if($agearr[0] < "21") {

//////////check against todays date, cause birfday peeps seem to be denied, due to seconds and decimal place mumbo jumbo

if(($_POST["month"] == date(m) || $_POST["month"] == date(n)) AND ($_POST["day"] == date(j) || $_POST["day"] == date(d)) AND ($_POST["year"] == (date(Y) - "21"))) {
//echo (date(Y) - "21");
$agearr[0] = 21;
}else{
$error_list[] = "<p>I'm sorry, but you cannot view this website, as you are not 21.</p>\n\n";
}
} else {}

if($_POST["day"] > "31") {

$error_list[] = "<p>There isn't even that many days.</p>\n\n";

} else {
}

if($_POST["month"] > "12"){

$error_list[] = "<p>There isn't even that many months.</p>\n\n";

}else{
}

if(date(Y) < $_POST["year"]){

$error_list[] = "<p>I'm sorry, but you haven't even been born yet. How'd you do that?</p>\n\n";

}else{
}

if(count($error_list) > 0) {

for ($checkme = 0; $checkme < count($error_list); $checkme++){
echo $error_list[$checkme];
}

echo "<p>Did you make a mistake? <a href=\"".$_SERVER["SCRIPT_NAME"]."\">Try again.</a></p>\n\n";

}else{
header("Location: contest.php");
}
}
have_footer();
?>

mhunt
08-31-2007, 05:18 PM
for some reason its not letting me edit that post, just replace the cookie stuff with the session crap from earlier, i was trying it with cookies but same results

CFMaBiSmAd
08-31-2007, 05:25 PM
So, what does the print_r() in the second - contest.php - file show exactly? And posting that code would help as well.

You can also turn on error reporting in a .htaccess file using the following -

php_value display_errors On
php_value error_reporting 6143



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum