$_SESSION Issue

Rayj00

New member
So after some searching, I found that using $_SESSION to pass a variable from one php page to another would suit my needs.

So here is what I am doing. I have an html form with multiple input.
I generate a jobnumber when the form is sent (submitted). But I want
to save the value of jobnumber for a PHP File 2. The problem is I am not getting
any response from the server in regards to what I am doing in File 1.

PHP File 1:
<?php
session_start();
// some code...
$dndid = $updatejobnumber; // the $updatejobnumber variable initialized a littler earlier in the code
$_SESSION['$dndjobnumber'] = '$dndid';
if (isset($_SESSION['$dndjobnumber'])) {
echo $_SESSION['$dndjobnumber'];
}

Any comments on why this is not working?
PS: I monitor the PHP via the firefox Web Developer Tool and the Netword Response tab.

Here is how I monitor for $_FILES:
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>"; This works fine.

But if I add print_r($_SESSION); after the print_r($_FILES) there is no change?

Comments?

Thanks,

Ray
 
  1. Variable assignment in PHP File 1:In your code snippet, you're assigning the string '$dndid' to $_SESSION['$dndjobnumber'] instead of the value of $dndid. Also, you're enclosing '$dndid' in single quotes, which means it will be treated as a string literal rather than a variable. You should remove the single quotes to properly assign the value of $dndid.
  2. Accessing the session variable in PHP File 2:Ensure that you are starting the session in PHP File 2 as well. Without starting the session, you won't be able to access session variables set in other PHP files.

<?php
session_start();
// some code...

// Assuming $updatejobnumber holds the value you want to store in the session
$dndid = $updatejobnumber;

// Assign the value of $dndid to the session variable without enclosing it in single quotes
$_SESSION['dndjobnumber'] = $dndid;

// Echo the value to verify if it's stored correctly
echo $_SESSION['dndjobnumber'];
?>

PHP:

And in PHP File 2, you should start the session before accessing the session variable:

PHP:
<?php
session_start();

// Access the session variable
if (isset($_SESSION['dndjobnumber'])) {
    echo $_SESSION['dndjobnumber'];
} else {
    echo "Session variable not set";
}
?>

Make sure both PHP files are properly linked, and the session is started before accessing or setting session variables. Also, ensure there are no errors in your PHP configuration that might prevent sessions from working correctly.

Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
 
Back
Top