PHP Saving Session Variable - php

I am trying a new CAPTCHA Script from here. To call the CAPTCHA code you use: $_SESSION['captcha']['code']
Well that works when I echo it out on the main form, but when I echo it after the form has been submitted, it displays a new code so I can never find out what the old code was when they submitted the form.
if($_POST['submit']) {
echo $_SESSION['captcha']['code'];
}
How can I save that session data and not make it change any more?

You should store it in your own SESSION variable:
$_SESSION['old_captcha'] = $_SESSION['captcha'];
Then, when the form is submitted, use you own variable:
if($_POST['submit']) {
echo $_SESSION['old_captcha']['code'];
}

Put that BEFORE you include the captcha.php again on the next step.

My guess is that it displays a new code because you come back to the page that displays it. Try submitting to a different page, or maybe not executing the captcha creation code if the session variable is already set.

Related

PHP SESSION cant store variable from GET form variable

<?
session_start();
$_SESSION['name'] = "$_GET["name"]";
$_SESSION['email'] = "$_GET["email"]";
$session_id=session_id();
echo"$session_id <br> $_SESSION['name'] <br> $_SESSION['email']";
?>
I am trying to create a session to store visitor input form with GET method, I cant use POST because the form is handled by wordpress plugins and the client only gave me the GET option. The problem is:
On page #1, this is the page after we submit the form, the echo is shown complete.
At page #2, I already add session_start(); at top but $_SESSION['name'] and $_SESSION['email'] keep empty (change page) but $session_id is stored and show same.
What am I missing? or maybe $_SESSION is cant store $_GET?
So on the first page you save the values from the user input and it works as expected.
However, when you go to the second page, your code is overriding the session with new variables, which are empty in this case. Before assigning anything to the session, you should check whether it is not empty / valid.
For this simple code I would recommend checking via isset / array_key_exists functions.

php $_POST variable is not saving

I currently have some php code on a form action, and it is being updated to the form's next page with
<?php echo $_POST['var here']; ?>
and it is working, but I noticed when Im trying to refresh the page it asks to confirm resubmission. When I resubmit it works in that tab in which it was sumbitted, but in another new tab it does not show the displayed php post variables. I even took it the next step by seeing that when I open the 2nd page after the form action has been submitted the php post variables are gone...
Help!
Thanks!
When you submit a form with <form method="post" /> it does a post request to the server, thus populating $_POST. When you open the link in a new tab it is no longer a post request but a get request. That is why you'll see nothing in $_POST.
$_POST — usually from forms
$_GET - from values on the URL (the query string myscript.php?myvar=Joe)
You can find plenty of resource about it. You can start here
If you want to keep the values you can save them to the session:
<?php
session_start(); // should be at the top of your php
if (isset($_POST['var'])) {
$_SESSION['var'] = $_POST['var'];
}
$myvar = isset($_SESSION['var']) ? $_SESSION['var'] : "no var";
echo $myvar;
Now the value is stored in the session so you can visit the page in a new tab and it will still be there.
This sounds like desired behavior. The $_POST variable should only be filled when the post action is created. If you're looking to store variables across pages you could store it in either the $_SESSION var in PHP or deal with the front end $_COOKIE business. If you're always going to be rendering pages from the backend then $_SESSION is the way to go. It's never too late to read up on cookies and sessions.
The skinny of it is that you're going to want to do something like this:
<?php
session_start();
if ($_POST['var']) {
$_SESSION['var'] = $_POST['var'];
}
echo $_SESSION['var'] ?: $defaultValue;
Then you'll notice that the message changes only when you post and won't exist before then.

$_SESSION: variable not carrying over to other php page

So basically I'm programming a form that passes data from index.php to highscore.php. To stop spammers that click on the submit button repetitively, I have added a system where index.php creates a uniqid and sets it as a session variable as well as posting it with the rest of the form. highscores.php receives the posted uniqid and compares it to the session variable. If it matches, it adds the data and unsets the session variable, thereby stopping all of the other posts it gets because the uniqid doesn't match anymore
However, the $_SESSION['form_token'] is not a valid index on the highscores.php page. I'm not sure what's going wrong...
Here is my code:
index.php
<?php
session_start();
$form_token = uniqid();
$_SESSION['form_token'] = $form_token;
echo $_SESSION['form_token'];
?>
highscore.php
session_start();
echo $_SESSION['form_token'];
The echo on the last line doesn't print anything.
If you need more code, please let me know.
Thanks in advance for your help!
EDIT: Running print_r($_SESSION) on index.php has returned:
56a741da4bcc7Array
(
[form_token] => 56a741da4bcc7
)
on highscores.php it returns
Array()
Also, on the index.html, the echo $_SESSION['form_token'] does return the uniqid...
EDIT 2: I've been experimenting with it, and i found out that if I reload index.php then submit the form, it works fine. Otherwise, it seems to not work...
Thanks again!

Weird SESSION issue on PHP

I'm having a very weird SESSION problem, I've been working with sessions in my server for a long time and everything working fine. I've searched & tried a lot of changes for 5 hours before posting this question.
Anyway, it seems that $_SESSION is not updating correctly. I have a form using POST that goes to the same page and when the user submits the info its saved in $_SESSION and then I do some stuff and update the session variable. Everything looks good in the page cause when I'm done I save the session with the new variables and then do a var_dump($_SESSION["whatever"]); and it shows the updated data. But when I submit the form again with new settings and show the data of $_SESSION in the reloaded page it's still the old data.
My code is kind of complex and I do a lot more than just save the data but I'm resuming the situation here:
session_start();
var_dump($_SESSION["whatever"]);
if(isset($_POST["whatever"])){
$_SESSION["whatever"]=$_POST["whatever"];
} else {
$_SESSION["whatever"] = FALSE;
}
var_dump($_SESSION["whatever"];
So, the below var_dump does show the new value entered in the form, that should mean it has been saved correctly, but no! Because when I enter the form again the first var dump that should have the same value as the last var_dump in the previous page still shows the original value (FALSE).
Any ideas?
UPDATE: Even if I go to a new page and just do this:
session_start();
var_dump($_SESSION["whatever"]);
It shows the old data, not the updated one.
PS: By the way I have many other variables that are not inside this if/else statement that do save correctly, and the session does update from the values on other previous page.
NEW UPDATE: I'm using jQuery to display the form with sliders and knobs, if I eliminate jQuery everything works well! It is very weird! I have seen nothing like this on the internet, any ideas?
session_start();
var_dump($_SESSION["whatever"]);
if(isset($_POST["whatever"])){
$_SESSION["whatever"]=$_POST["whatever"];
} else {
// Check if there is a session variable set
if (!isset($_SESSION['whatever'])) {
// If the session variable isn't set, initialize it to false
$_SESSION["whatever"] = FALSE;
} else {
// Set the $whatever variable to be the value of the session variable
$whatever = $_SESSION['whatever'];
}
}
var_dump($_SESSION["whatever"];
<!-- In the HTML -->
<input name="whatever" value="<?php echo $whatever ?>" type="text">

Unset POST values in php

acitvity.php
//Form start
<form action=''>
</form>
//Form End
//Get POST Values
<?php
$_POST[''];
?>
//End
if i refresh the page after form is submitted, all the posted values are resubmitted, reason because all values are in browser so they are resubmitted. When i was searching solution for this, i got info that if the form & post operation done in separate php file then no more issue in posting values on refresh.
Is this the solutions? but now i have to do both in single file & POST values should not be submitted again on refresh.. is there any way to do this???
Learn PRG Pattern so that you can do this properly :)
http://en.wikipedia.org/wiki/Post/Redirect/Get
For example, you are trying to handle a user registration form, so what you do is you get a bunch of POSTed values, and save it into your database.
if(!empty($_POST)) {
// validate and save to db
// get last inserted user_id
}
After you do that, instead of returning the same page with the previously POSTed values, you redirect the new user, for example to his profile page (assuming you have no activation requirement in place)
if(!empty($_POST)) {
// validate and save to db
// get last inserted user_id, say in $user_id
header("Location: /users/$user_id");
}
That way, the browser redirects and you won't have problem with say, double registration, whenever the user hits refresh.
After saving to your database, reload your page:
if ($_POST) {
// Save $_POST to database and other stuffs
// Reload current page to discard $_POST
header('Location: my_page.php');
}
That's called PRG or Post/Reload/Get
You can use unset($var) to unset a variable. However, I think the issue is with the browsers; some of them try to be smart and will remember form data regardless when you refresh the page. If you hit "go" or "enter" on the URL bar it does a "true" refresh though.

Categories