Solved

Passing Session variable not working

Posted on 2010-08-13
5
300 Views
Last Modified: 2012-05-10
Hi everybody,

could you tell me why my session variable is not working?  I've attached part of the code im using.

File 1 (Creates a random number and displays it as a picture)
File 2 (Pull Session variable for validation)

The whole script works fine except for that session variable not being able to be passed.

<?php
session_start(); 
$_SESSION['rand'] = rand(10000, 99999);
$image = imagecreate(60, 30);
$bgColor = imagecolorallocate ($image, 255, 255, 102); 
$textColor = imagecolorallocate ($image, 0, 0, 0); 
imagestring ($image, 5, 5, 8, $_SESSION['rand'], $textColor); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 

header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 

header("Pragma: no-cache"); 

header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>

Open in new window

<?php
session_start();

$mailto = 'steve@remotely-connected.com' ;

$subject = "Feedback Form" ;

$formurl = "http://www.nationalrepocenter.com" ;
$errorurl = "http://www.remotely-connected.com/error.html" ;
$thankyouurl = "http://www.remotely-connected.com/ty.html" ;

$email_is_required = 1;
$name_is_required = 1;
$telephone_is_required = 1;
$password_is_required = 1;
$uself = 1;
$use_envsender = 0;
$use_sendmailfrom = 0;
$use_webmaster_email_for_from = 0;
$use_utf8 = 1;

// -------------------- END OF CONFIGURABLE SECTION ---------------

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
if (isset( $use_sendmailfrom ) && $use_sendmailfrom) {
	ini_set( 'sendmail_from', $mailto );
}
$envsender = "-f$mailto" ;
$fullname = $_POST['fullname'];
$company = $_POST['company'];
$address = $_POST['address'];
$email = $_POST['email'] ;
$telephone = $_POST['telephone'];
$fax = $_POST['fax'];
$chapka = $_POST['password2'] ;
$give = $_SESSION['password'];

$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
	header( "Location: $formurl" );
	exit ;
}
if (($email_is_required && (empty($email) || !preg_match('/@/', $email))) || ($name_is_required && empty($fullname)) || ($telephone_is_required && empty($telephone)) ) {
	mail($mailto, $subject, $messageproper, $headers, $envsender );
	mail($mailto, $subject, $messageproper, $headers, $envsender );
	header( "Location: $errorurl" );
	exit ;
}
if ( preg_match( "/[\r\n]/", $fullname ) || preg_match( "/[\r\n]/", $email ) ) {
	mail($mailto, $subject, $messageproper, $headers, $envsender );
	header( "Location: $errorurl" );
	exit ;
}

if (empty($email)) {
	$email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;


$messageproper =
	"This message was sent from:\r\n\r\n" .
	"-----REMOTELY CONNECTED WEBSITE --------------------------------\r\n" .
	"\r\n" .
	"Sender's name:          $fullname\r\n" .
	"Sender's Company:       $company\r\n" .
	"Sender's Address:       $address\r\n" .
	"Sender's Email address: $email\r\n\r\n" .
	"Sender's Phone number:  $telephone\r\n".
	"Sender's Fax number:  $fax\r\n".
	"$chapka || $give  \r\n\r\n" .	"\r\n\r\n--------------------------------------------------------------\r\n" ;

$headers =
	"From: \"$fullname\" <$fromemail>" . $headersep . "Reply-To: \"$fullname\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.15.0" .
	$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;

if ($use_envsender) {
	mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
	mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;

?>

Open in new window

0
Comment
Question by:tekguy2001
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 33429304
I'm confused. In file1 you use $_SESSION['rand'] and in file2 you use $_SESSION['password']

There is no session information in common other than the session itself and sessions have no data unless you put it in there.
0
 
LVL 11

Assisted Solution

by:Pieter Jordaan
Pieter Jordaan earned 250 total points
ID: 33429328
Hi

Place the attached code at the bottom of each page.
This will display the contents of $_SESSION

Please post the output.
debug($_SESSION);

function debug($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}

Open in new window

0
 

Author Comment

by:tekguy2001
ID: 33430872
Sorry, I just fixed that and it still doesn't work.  Can a hosting site disable that feature in PHP?  I've followed everything to the letter and it still doesn't work.  I've also put that code from  bitfreeze and it doesn't display anything.  I've also created a test.php which is called from the form and it has the following code

It should work no?  It should at least shwo the variable.
<?php
session_start();
echo ('here');
echo $_SESSION['rand'];
print_r($array);
echo "</pre>";

?>

Open in new window

0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33430914
Lets just check if it can save and retrieve a value.

<?php
session_start();

$_SESSION['testentry'] = "working";
echo $_SESSION['testentry'];

echo "</pre>";
print_r($_SESSION);
echo "</pre>";

?>
0
 

Author Closing Comment

by:tekguy2001
ID: 33431004
Thanks to you both, but it looks like the Host provider disabled the option...  Grrr.  Well it's all fixed and working now, thank you.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

776 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