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 "
Related
$_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?
In a subpage on my drupal site I have a form. I'd like the form to post to a simple process.php file, that takes the form data and then sets a $_SESSION variable and returns the user to the original page.
Currently the process.php file is not seeing the $_SESSION vars set in standard drupal pages and viceversa, drupal pages don't see the $_SESSION vars set in the process.php file, they act as if they are on completely different sites. What am i missing?
From my research it looks like the only way to set session variables and run your own php code is to create a module.
I've looked at the Drupal.org tutorial here:
https://www.drupal.org/developing/modules
and this one here:
http://www.wdtutorials.com/2012/04/29/drupal-7-how-create-module-part-1
Lastly, I'd also recommend looking at existing modules and learning from those as well.
I currently have a site I built using jquery/php/PDO/mysql.
I use classes for most functions, including database, logins, content, etc...
I am wanting to change my forms over to jquery's ajax calls. But there's where my problems start. With the ajax call, I can't call a php function in the ajax post url. Heres the heirarchy Im using;
->content.php (form resides in a function named content.)
->process.php (post checks and then a call to add content from class)
->class.content.php (insert vars into db)
Once form is submitted, it goes to process.php which contains checks and then a class call to add content.
While this hierarchy seems to be the most used for ajax, it causes path issues. It breaks my db connection, my config connection, etc...
All I really want is to add ajax forms. But I don't want to rewrite my whole site. Any suggestions?
It sounds like process.php was previously included in content.php but now you're trying to submit to it directly. However, process.php was dependant upon the configuration variables, database connection etc. which was included in content.php and accessible by being included within that page.
I think you need to make a new page which includes the same other files and has the same initialisation code as content.php, except it doesn't output the form and instead just includes process.php, then submit to the new page.
Hard to say for sure without code though.
In my code I have a link:
<a href='http://www.anotherpageinmywebsite.com'>Click for the other page</a>
When the user clicks this link I need to save a value into a PHP session variable. I don't want to use Ajax, nor an iframe, nor a GET variable (I don't want the session variable appearing to the user as it is sensitive data), EDIT nor do I want to put all the links on the page inside a form.
It seems like it should be possible -- because after all, clicking the link generates a call to the web server to load the new page, and shouldn't I be able to add some code (somehow) to "piggyback" on the natural process a link goes through to post a 'load this new web page' request to the web server?
How can I 'piggyback' on this naturally-occurring sequence of events when the user clicks a link to update a PHP session variable (without using GET variable, ajax or an iframe, preferably).
If we assume that you're using some PHP based framework and you have a controller which serves the pages according to URL - it's quite trivial in fact.
Assuming you are using CodeIgniter - but the logic would be the same for most MVCs out there.
<?php
class Web extends Controller {
// this will fire when you access http://mysite.com/mypage
function mypage($encoded_value) {
$decoded_value = decode($encoded_value);
// although CI has a different way of doing this
$_SESSION['mypagesessionvar'] = $decoded_value;
$this->load->view('mypage'); // this loads the actual page
}
}
I wanted to utilize the default behavior of the browser when a user clicks a link which calls back to the web server -- I wanted to 'piggyback' on that default behavior and send an updated session variable. I was trying to avoid an extra call to the server to update my session variable and I was under the requirement "no GET variables are allowed."
In the end we punted and we coded an Ajax call to set the session variable. We were not able to figure out how to 'piggyback' onto the default behavior of clicking the link without using a GET variable.
Is it possible to use a variable from one page in a piece of code on another? e.g. submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table
Thanks for all the help
Your best bet would be to use PHP's session functions. That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). You can get more information here -- PHP Sessions. Basically all you have to do is call
session_start();
at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. You can then set/retrieve a variable using
// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];
This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php
The script that generates the form does not have to be the script the processes the form. all it takes is for the FORM tag to point to the correct script. The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST).
If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. At a simplistic level, you can have the first include() the second so it has access to all the same data. A bit more advanced is to re-create the original POST and synthesize a POST. This will probably require some JavaScript assistance. Another approach is to use PHP's session feature and put the value aside so a later script has access to it. This relies on the user then following the correct link, however!
Use session to access your variable to the second/other page.
Example:
index.php
<?php
session_start();
$_SESSION['yourData'] = 'yourData';
?>
=========================================================================
second_page.php
<?php
echo $_SESSION['yourData'];
?>
You may try this one. Visit this link for more details. Or PHP's official website.