Jump to content

passing session data between scripts w/o refresh page


koolaid

Recommended Posts

ok i am stumped.

 

I have a php page. This page calls 2 scripts w/o refreshing the page.

 

<?php

if (session_id()) {
$_SESSION['theresumename'] = "yet to be defined";

}else{

session_start();
$time = time();
$date = $today = date("Ymd");
$id = $time + $date;

session_id($id);
print session_id();
$_SESSION['theresumename'] = "session just started";
echo $_SESSION['theresumename'];

} 

?>

 

 

then when the user hits an upload button it successfully runs this script w/o refreshing that page

if(!is_dir("./files")) mkdir("./files/", 0755); 
//move the uploaded file
$replaceArr = array(" ", "#","!", "*", "^", "+");
$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);
$filename = "./files/".$name;
srand(time());
$random = (rand()%9999);
$ext = substr(strrchr($name, "."), 0);
$name2 = str_replace($ext,'',$name);
$arandomname = $name2.$random.$ext;
$nizame = $arandomname;

$_SESSION['theresumename'] = $nizame;

 

EVERYTHING TO THIS POINT WORKS,

then when the user hits submit it runs an email script. All the email script works accept for the one session variable. i don't understand.

session_start();  

if(isset($_SESSION['theresumename'])){
    

}else{

    $_SESSION['theresumename'] = 'was not set';

}

 

Thank you in advance. i have been trying to pass this one variable since 9 in the AM. I am at a loss.

Tnx guys good catch.

Well, now i am at where i was this mprning. It is passing the session data but it changes from what it should be to a random number.

Not sure why.

 

 

This script will successfully writes the file to my server and will name it like this

 

"filename0000.jpg"

 

the zeros being a random #;

 

but when i reference the var $_SESSION['theresumename']

in the next script it returns just a random 4 digit number, and not the same random 4 digit.

 

<?php
session_start();
error_reporting(E_ALL);

//create the directory 
if(!is_dir("./files")) mkdir("./files/", 0755); 
//move the uploaded file
$replaceArr = array(" ", "#","!", "*", "^", "+");
$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);
$filename = "./files/".$name;
srand(time());
$random = (rand()%9999);
$ext = substr(strrchr($name, "."), 0);
$name2 = str_replace($ext,'',$name);
$arandomname = $name2.$random.$ext;
$nizame = $arandomname;

$_SESSION['theresumename'] = $nizame;
echo $_SESSION['theresumename'];

if (file_exists($filename)) {
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_SESSION['theresumename']);
chmod("./files/".$_SESSION['theresumename'], 0777);

}else {
    move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$name);
chmod("./files/".$name, 0777);
}

if ( !($_SESSION['theresumename'] === $theresumename) )
	$_SESSION['theresumename'] = $theresumename;
	echo $_SESSION['theresumename'];


?>

The initial page:

<?php
session_start(); 
error_reporting(E_ALL);

if (session_id()) {


}else{

$time = time();
$date = $today = date("Ymd");
$id = $time + $date;

session_id($id);
print session_id();
echo "time = $time\r\n";
echo "date = $date\r\n";
$_SESSION['theresumename'] = "page opener";
echo $_SESSION['theresumename'];

} 

?>

 

The first script i run

The $_SESSION['theresumename'] is set under the comment //move the uploaded file

[/code]

<?php

session_start();

error_reporting(E_ALL);

 

//create the directory

if(!is_dir("./files")) mkdir("./files/", 0755);

//move the uploaded file

$replaceArr = array(" ", "#","!", "*", "^", "+");

$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);

$filename = "./files/".$name;

srand(time());

$random = (rand()%9999);

$ext = substr(strrchr($name, "."), 0);

$name2 = str_replace($ext,'',$name);

$arandomname = $name2.$random.$ext;

$nizame = $arandomname;

 

$_SESSION['theresumename'] = $nizame;

echo $_SESSION['theresumename'];

 

if (file_exists($filename)) {

move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_SESSION['theresumename']);

chmod("./files/".$_SESSION['theresumename'], 0777);

 

}else {

    move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$name);

chmod("./files/".$name, 0777);

}

 

if ( !($_SESSION['theresumename'] === $theresumename) )

$_SESSION['theresumename'] = $theresumename;

echo $_SESSION['theresumename'];

 

 

?>

[/code]

 

The second script i run

<?php

session_start(); 
error_reporting(E_ALL); 

$sendTo = "[email protected]";
$subject = "RESUME SUBMISSION (applicant)";

$headers = 'From:' . $_POST["email"] . "\r\n" .
    'Reply-To:' . $_POST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$headers .= "Content-type: text/html\r\n";

$path =  $_POST["mypartialurl"];
$partial = $path ."files/";
$finalname = $_SESSION['theresumename'];
$linktosend = $partial.$finalname;

$message = 'RESUME SUBMISSION,

name -'. $_POST["name"] . "\r\n \r\nTHE SUBMITTED INFO: \r\n \r\nAre you a creative thinker and problem solver? - " . $_POST["quest1"] . "\r\nDo others consider you a natural born leader? - " . $_POST["quest2"] . "\r\nAre you ready to set your own income potential? - " . $_POST["quest3"] . "\r\nAre you ready for responsibility from day one? - " . $_POST["quest4"] . "\r\nemail - " . $_POST["email"] . "\r\n\r\nAdditional comment / question - " . $_POST["message"] . "\r\n\r\nlink to uploaded resume:\r\n" . $linktosend; 

mail($sendTo, $subject, $message, $headers);


?>

Thanks for your help man. It has to be Defined because my

 

if statement (on page 2) writes the file to the server

 

if (file_exists($filename)) {
move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_SESSION['theresumename']);
chmod("./files/".$_SESSION['theresumename'], 0777);

 

You know  a lot more then I but this bit of code from page to is what names the file that the above code writes to the server it starts with the file that is selected from when the user browses and selects a file to upload:

$_FILES['Filedata']['name']

 

The file name is posted from Flash. i know that part is working because it writes the file to my server with the proper file name.

 

//move the uploaded file
$replaceArr = array(" ", "#","!", "*", "^", "+");
$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);
$filename = "./files/".$name;
srand(time());
$random = (rand()%9999);
$ext = substr(strrchr($name, "."), 0);
$name2 = str_replace($ext,'',$name);
$arandomname = $name2.$random.$ext;
$nizame = $arandomname;

$_SESSION['theresumename'] = $nizame;
echo $_SESSION['theresumename'];

I don't mean the session isn't defined i mean the variable called $theresumename isn't defined.

 

Look at the bottom of your 2nd script

 

if ( !($_SESSION['theresumename'] === $theresumename) )

      $_SESSION['theresumename'] = $theresumename;

 

What is it comparing against :S

I see. That was a bit of code that i found on a forum from someone with a similar paroblem. I would think it should work is i removed that entirely right? Cause don't i define $_SESSION['theresumename'] with this code?

 

$replaceArr = array(" ", "#","!", "*", "^", "+");
$name = str_replace($replaceArr, "_", $_FILES['Filedata']['name']);
$filename = "./files/".$name;
srand(time());
$random = (rand()%9999);
$ext = substr(strrchr($name, "."), 0);
$name2 = str_replace($ext,'',$name);
$arandomname = $name2.$random.$ext;
$nizame = $arandomname;

$_SESSION['theresumename'] = $nizame;
echo $_SESSION['theresumename'];

 

or should i change the bit of code you are refering to, to this:

if ( !($_SESSION['theresumename'] === $nizame) )
	$_SESSION['theresumename'] = $nizame;
	echo $_SESSION['theresumename'];

 

I am sorry i am so confused. I think i understand this less now then i did 10 hours ago.

Sessions and variables are not the same

 

This is where you define the session

 

$_SESSION['theresumename'] = $nizame;

 

But with this code

if ( !($_SESSION['theresumename'] === $nizame) )

$_SESSION['theresumename'] = $nizame;

 

You might be overwriting the session with something new, check all your variables are what you think

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.