Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Can't pass a value through session

Jevgeni

New Coder
If I pass the string trough session it perfectly works



[CODE lang="php" title="1.php"]<?php
session_start();

$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>
[/CODE]

[CODE lang="php" title="2.php"]<?php
session_start();
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>
[/CODE]

But if I pass a value inserted into the form field it doesn't work.

[CODE lang="php" title="1.php"]<?php
session_start();
?>
[/CODE]

HTML:
<form id="auth" method="post" action="2.php">
<div id="t20_1" class="t s2_1"><div class="norm2"><input type="text" name="report_no" form="auth"/></div></div>
<div id="t21_1" class="t s6_1"><input type="submit" value="Add data" name="upload" form="auth" id="add_data">Recording level</div>
</form>

PHP:
<?php

$_SESSION['report'] = $_POST['report_no'];

?>

In the file 2.php I get the value NULL. Can somebody explain how to solve this issue?
 
What does your full file with the form look like? It looks to me like you are missing the session_start() in the second example if these are separated out into separate files, but that could just be the way you have the code split up.

Are you starting the session before outputting the form? Starting the session should be done before the browser receives any output from your website.
 
If I pass the string trough session it perfectly works



[CODE lang="php" title="1.php"]<?php
session_start();

$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>
[/CODE]

[CODE lang="php" title="2.php"]<?php
session_start();
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>
[/CODE]

But if I pass a value inserted into the form field it doesn't work.

[CODE lang="php" title="1.php"]<?php
session_start();
?>
[/CODE]

HTML:
<form id="auth" method="post" action="2.php">
<div id="t20_1" class="t s2_1"><div class="norm2"><input type="text" name="report_no" form="auth"/></div></div>
<div id="t21_1" class="t s6_1"><input type="submit" value="Add data" name="upload" form="auth" id="add_data">Recording level</div>
</form>
2.php
PHP:
<?php

session_start();


$_SESSION['report'] = $_POST['report_no'];

?>

In the file 2.php I get the value NULL. Can somebody explain how to solve this issue?
 
What does your full file with the form look like? It looks to me like you are missing the session_start() in the second example if these are separated out into separate files, but that could just be the way you have the code split up.

Are you starting the session before outputting the form? Starting the session should be done before the browser receives any output from your website.
The issue is solved. I passed value using GET.
 
edit:
nevermind, OP fixed it... He was sending GET but checking POST

If your session value is saving, but the $_POST is null then you need to see what's being sent by the form.
Can you do a print_r($_POST) on 2.php to check the form is sending data? If the form IS sending the correct information, but it's not saving to session then keep reading, I think I know why.

If you mean that the report session is NULL / not set later on then it's most likely it's because of the whitespace before your session_start()
Depending on your setup, this whitespace (blank space) will count as content being sent which prevents your session_start().

To fix this, make sure your session start is always the first output (even before spaces) by PHP like so:
Code:
<?php
session_start();
$_SESSION['report'] = $_POST['report_no'];
?>

If this still does not work then you will want to put some error handling in place to see what's going on...
Code:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Additionally I highly recommend doing some value checks.
Code:
<?php
session_start();
$_SESSION['report'] = isset($_POST['report_no']) && FILTER_VAR($_POST['report_no'], FILTER_VALIDATE_INT) ? $_POST['report_no'] : false;
?>
This will allow you to then go and and check if the report session == false or not. If you don't do any sort of check before setting the session then there's no way to guarantee that the report number is actually a valid integer... You would have to check the session type each time, so it's better to just check it before you set it so when you check it again on another page, you know it's the valid type.
 

New Threads

Buy us a coffee!

Back
Top Bottom