PHP $_SESSION - some values retained, others lost during page submit - php

$_SESSION is successfully maintaining some fields across a page submit process, but ONLY those that are specifically conserved in my own, self-created $_SESSION initialisation function.
This would not be odd if my $_SESSION initialisation function was actually being called .. obviously!! Its whole purpose is to retain some key values, and wipe all the others from the $_SESSION array
However, this happens even when that function is NOT called by my code.
I have a View page including a form that is submitted, identifying a Control action file to trigger on submit.
I verify $_SESSION contents as part of the View page display. It displays the full set of $_SESSION array fields
I then do a print_r of $_SESSION contents immediately following session_start() in the identified Control file
Prior to the user hitting submit, the full set of $_SESSION values are there
Immediately after user hitting submit, most of them are gone, but anything I choose to retain on $_SESSION initialisation is retained.
NONE of my own code can possibly be running in between: there is pure PHP only there.
I had 2 theories on this.
1. PHP does something in the background and happens to have an internal function name that I used by mistake, which was then calling my function in preference to the system function of the same name, thus resulting in my $_SESSION getting initialised.
So I changed the function name (to clean_session()) -> same problem
Something about running the function when I initialise the $_SESSION upfront at the start of the application is giving those fields within the $_SESSION array a different set of properties from other fields which are added later. As a result, only those fields are surviving a generic loss across the page submit.
Option 2 seems like the only even potentially non-insane answer.
Does anyone know if that is even remotely possible?
Or alternatively what else could be happening?

Related

PHP $_SESSION lost over several pages

I am setting session_start() on every page as very 1st statement.
The 1st page sets $_POST['myVar'] when the page ist submitted. The 2nd page evaluates this by a php lib which sets $_SESSION['myVar'] if this is set in $_POST and not set already in $_SESSION.
When submitting this form and calling the 3rd one evaluating there $_SESSION['myVar'] results that the variable is no more set! There is no session_unset() called between.
Theoretically all is correct but the use shows the opposite. What may there be wrong or still missing?
In the page you have a form, probably your first page session is not required. In the next page(form action page), you need to start the session first. Then you can give like $_SESSION['fieldName']=$_POST['fieldName']. After this in whichever pages you use session_start(), you can have the data. Hope this helps..

How to check multiple form submission with PHP?

I have my own MVC applicaiton where I have 3 main area: model, view, controller.
In the view, I've created a form, to insert some data to the DB. The action is set to the same page and the type is: POST.
In the model, I have some basic checking if the form has been submitted or not. There is an 1 second sleep after that.
In the controller, I'm connecting the view and the model in a very simple way, so they can commuicate between each other.
However a problem is arising, whenever the visitor clicks multiple times on the submit button. Because there is 1 second waiting (it is intentional - so it looke like the system is processing the data), they can click multiple times to the button.
There is an issue with that, because this way, the data gets inserted to the database based on the amount of clicks. If I output a simple word, like "Foo" it will get displayed only 1 time (I guess this is because the page is reloading every time), however the data gets inserted multiple times to the DB.
Is there any way around this issue? My intention was to create a session and check if this is the first submit, else do nothing with the DB. However this didn't work, because the session always got the basic value upon the page load.
Can anyone help me with this issue?
Two options:
Put some javascript on the front-end to disable the button upon being clicked and then submit the form - this is probably the easiest way but you may want to try and leave the front-end as uncluttered as possible.
Set a session variable before you load the page, then when you do the insert delete this session variable immediately after. Before doing the insert you should check for the session variable - if it is set then the insert is allowed, if it is not then it isn't. You may also want to add in code into a master controller (if all your controllers are derived from one master controller) that checks what controller has been called and then if the session variable that is set should be for a different controller then it should be deleted (so it no longer exists if the user goes to a different page without submitting the form).

PHP Session - Values Randomly NOT Being Written

I am building a site that combines ajax and PHP to create dynamic forms that load/validate/submit in-page. I am using a combination of PHP session variables with custom handlers, $.post, and $().load to accomplish this. I build my forms in individual php pages (they are very large) and then load them into a section of my main page.
At the top of my main page I start a session like so:
<?php
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
?>
Then in each form page I start a session and generate a token variable, like so:
<?php
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
$_SESSION['token'] = rand();
?>
This token variable gets inserted as a hidden field at the bottom of the form page, and should write to the session.
However, I've found that session variables set in dynamically loaded (or posted) pages do not seem to consistently stay set. They work within the scope of the form page but then do not always carry over back to the main page. I thought this might be because they were not actually accessing the right session, but it seems all session variables outside the form page can be accessed within the form page.
So I am stumped. The PHP handler I am using is the one here: http://www.zimuel.it/en/encrypt-php-session-data/
Edit: When I load one of the form-pages directly, it writes 100% of the time. It is only when I load a form via jQuery that it gets wonky.
Edit2: The issue wasn't that I was declaring the code in the wrong order. I did some research and found that I am supposed to declare the save path and handler functions before I start the session. The issue appears to have been either that I opened php with "

Codeigniter select box not populating session value

I am facing a session problem with Codeigniter.
I have a form containing a select box to perform a search. When the form gets submitted, the search criteria is stored in session, which is used to populate the form fields once user gets back to the form. In my local machine, it works fine. But in server, it doesn't for select boxes. Upon clearing the browser cache only, it works.
I have just echoed the session variable. outside the form. And it holds the correct data.In Firebug, it shows the correct value also - <option selected="selected">foobar</option>. But I can't see the selected value in the select box.
FYI - I am using the following CI form helper to print select box.
<?php echo form_dropdown('product', $search_options['product_list'],
stripslashes($this->session->userdata('qes_product')),
'id="qes_product" tabindex="1"');
?>
Any idea ?
1) Make sure you're using DB storage for your session and not a cookie. Cookies have a limited size, and if you're doing something wrong, meaning not replacing the values but constantly adding them to the session, then it can get full. This is something else you should look into (what you store in your session)
2) Don't use $this->session in your view. Store everything in a array (like $formdata) in your session, then retrieve it in your controller and pass it to your view.
You can access it in your form using $formdata['ques_product']
If above doesn't help, try changing the value of sess_expire_on_close in your session config. There seems to be weird a bug in CI.
Good luck!

PHP:How to save checkbox values temporary before submission

I'm a bit rusty in PHP,
I'm curently doing an PHP assignment where a user can select and save listed images/data/values into their own collection.
I have inputted all the data and printed it out in a repeat region with recordset paging.
I'm confused about how I am supposed to save a checked checkbox temporary before submission as there a more then 1 page as I'm using recordset paging to output the options.(Meaning: i have selected 2 values in the first page then i click next page to select balance values and finally submit my selection)
TIA
I have read an article on storing in session , that is the solution I guess, but I wonder how I'm supposed to send the value to the session when chaging the page (recordset paging generated by Dreamweaver)
To clarify the previous answers, you will most likely want to create a new $_SESSION variable for each check-box and associate a boolean with it.
You can store the result of a form post in PHP's $_SESSION variable.
Read this post for more information: Storing Form Data as a Session Variable
Also, there has to a tutorial or something in Google Land.
If you need to save the form results without submitting the form, try a JavaScript/AJAX approach. The idea is that you actually do submit the form, but in a behind-the-scenes kind of way (that is, the user never notices it). Essentially, you're going to want to build a "autosave" functionality. Look at these two posts:
Autosaving Form Input Using Prototype and PHP
AJAX Autosave functionality
They probably won't fit your needs exactly, but they should give you a good idea of what you should do.
Note: both of these posts use a timer to trigger their autosave functionality. I would suggest tweaking the trigger to detect any changes in your form instead.
Store them in _SESSION and process when needed.

Categories