I have a page with checkbox, every checkbox can be set on true, if on true, the products id is added across an ajax inside an array.
I look my ajax and it work's well.
my ajax
session_start();
if (is_array($_POST['product_id']) && isset($_POST['product_id'])) {
$_SESSION['productsCompare'] = $_POST['product_id'];
}
$_SESSION['productsCompare'] return an array with the products id values
After selected some checkbox, if I click on button to go another page and I wrote var_dump($_SESSION['productsCompare']), I have null as result.
Do you any idea to resolve this issue ?
Thank you.
Please make sure that on the other page where you want to get the values from the $_SESSION variable, you have started the session by calling the session_start(); and this is the very first line of your file.
EDIT:
Suppose I have two files:
<?php
session_start();
// index.php
$_SESSION['foo'] = 'bar';
<?php
session_start();
// sess.php
var_dump($_SESSION['foo']);
It prints /sess.php:4:string 'bar' (length=6)
So please make sure that you have started the session on the other file as well.
Related
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!
I currently have a form that allows a user to edit entries. However, it doesn't seem to be possible to use 1 (specific) cookie per tab. Whenever a user edits an entry, the record in the last tab gets updated.
I've tried the following in my main script (eventfilters.php):
<?php
$cookie_name = $_SESSION['username'].md5(time());
session_name($cookie_name);
setcookie(session_name($cookie_name),session_id(),time()+"300");
if(!isset($_SESSION)){ session_start(); }
if (isset($_GET['edit'])){
// Pass cookiename in url variable $cookie, so it gets caught by $_GET['cookie']
echo '<form action="eventfilters.php?save&cookie='.session_name().'" method="post">';
} else if (isset($_GET['save'])){
if(isset($_GET['cookie'])){
error_log("SAVE ".$_GET['cookie']); // Displays cookie url variable set by form action.
error_log("LOW ".$_SESSION['level_low']);
// Displays correct session value received from ajax
}
}
?>
`
The ajaxcode (also in eventfilters.php) contains this (called few times when page is already loaded):
$.post("include/severitygroups.php",{'cookie_name': "<?php echo $cookie_name; ?>", 'serialized_sev_groups': serialized_sev_groups}, function(data){});
This seems to pass the right cookiename to the other script, which successfully seems to return $_SESSION['level_low'] (as it appears in the error_log).
<?php
include('pdodb.php');
if(!isset($_SESSION)){ session_start(); }
$cookie_name = $_POST['cookie_name'];
error_log("SCRIPT ".$cookie_name);
error_log("COOKIEDATA ".$_COOKIE["$cookie_name"]);
// populating $_SESSION['level_low']
?>
It seems that the $_GET['save'] is populating the wrong sessions (initialised by the last loaded instance of eventfilters.php), even when the $_GET['save'] logs the right $_SESSION['level_low'] to the errorlog.
What is going wrong?
You can't do it. The best way to do it, is with Ajax, so, you will pass an "ID" to each tab (or page) like:
editPost.php?id=someID
And, each time you press save, you should send your content with your id param to save it.
I can pass values form one page to another but I need to pass value like this,
Page 1:
Page2.php
Page3.php
I need to pass the radio button values in the Page1.php to Page2.php, and also i need to use same session . if the form is redirected to page3, I am unable to get the value of page 1. its online quiz project. I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
The best practice would be to go with OOP. So, you can do the following:
Create a class with fields to hold all the information you want to pass.
class PageInfo
{
var $pageTitle;
var $currentPage;
var $btnValue;
.....
}
2.Now crate an object of PageInfo class, and assign the values you want to set.
$page = new PageInfo();
$page->pageTitle = "Home";
$page->btnValue = 1;
...
3.Assign this object (holding all the details of your page) to the super-global session variable.
// store session data
$_SESSION['page'] = $page;
4.Now you can access the value stored in the session at the different pages.
$otherPage = $_SESSION['page'];
echo $otherPage->pageTitle;
echo $otherPage->btnValue;
Note: The session_start() function must appear BEFORE you print anyting on the page.
Actually your question is vague, first of all when you are using sessions, you need to be sure you are calling session_start() at the very top of the page, secondly, you can save the form data in your session variable like
if(isset($_POST['YOUR_SUBMIT_BUTTON_NAME_HERE'])) {
$store_temp_val1 = $_POST['whatever']; //Sanitize the value first
$_SESSION['page_one']['first_val'] = $store_temp_val1;
}
Now you can simply retrieve the value stored in the session variable on the second page like
echo $_SESSION['page_one']['first_val']; //Will echo the previous page value
Note: Use session_start() on each page at the very top.
Whenever I go to a page i.e. login page or any other page, I want to save the name of the page in a $_SESSION variable.
login page:
<?php
session_start();
$_SESSION['page'] = 'login.htm';
?>
It works only for the login page and doesnt overwrite in other pages for e.g. home page:
<?php
session_start();
$_SESSION['page'] = "home.htm";
?>
I need the sesssion variable 'page' to hold the last page I was, can anyone help please?
Why not just use $_SERVER['HTTP_REFERER']? This will give you the previous page in PHP, without having to add anything to sessions.
when you navigate to a new page first retrive the saved "back" variable (and use it in a back link/breadcrumbs or something), and then overwrite the sessions "back" variable with the curent page, to have it ready for the next move =)
If all you need is default "back" functionality you should let the browser handle it.
If what you want is something to be used as a breadcrumb following some internal order (or path in a tree) my advice is to let each page "know" the path that leads to it.
If you really need to know from what page the user came from save it to a previous variable before you write over the current variable.
// Make sure user didnt just refresh the page
if ($_SESSION["current"] !== "currentPage.php") {
$_SESSION["previous"] = $_SESSION["current"];
$_SESSION["current"] = "currentPage.php";
}
You're using different keys.. 'page' and 'back'.
I have created a page with name edit.php. I have moved on this page from action.php using a edit button. I have successfully retrieved the values in the respective text boxes and other form items. I have problem that if by mistake this edit.php page is refreshed all values are gone. What is other way to maintain the values? Though thing are going well if page is not refreshed. If session variable is created than how values are retrieved of both that is of session variable and from database?
What I have did with problem.. I have requested "albumid" on action.php page..
session_start();
$aid = mysql_real_escape_string($_REQUEST['albumid']);
Now if action.php page is requested through edit.php page using edit button. than I have created a session variable. and destroyed it after successful update query.
if (isset($_POST["edit"])) {
$_SESSION["aid"]=$aid;
$result= mysql_query("SELECT * FROM table WHERE a_id =".$_SESSION["aid"]) or die(mysql_error());
$row=mysql_fetch_array($result); }
It means now session is created.. if page is refreshed than also session values remains and accordingly values are selected from this variable.
if($_POST['update']!="") {
Update query
session destroyed }
Than also my problem is not solved that is if page is refreshed before hitting update button I am loosing all values.
session variables are just data you put into the $_SESSION superglobal. Accessing them is no different than accessing any other array, except that the session array is saved for you automatically. All you need to remember is to do a session_start() before doing anything with the session.
$_SESSION['formfield1'] = $_POST['formfield1'];
$_SESSION['formfield2'] = $_POST['formfield2'];
etc...
<input type="text" name="formfield1" value="<?php echo htmlspecialchars($_SESSION['formfield1']) ?>" />
By default, PHP uses file-based sessions. To put it into a database, you'd have to write your own session handlers and point PHP to them using session_set_save_handler().
When you submit, save the values in appropriately named $_SESSION members. Then on your page, if you can't find the members in $_GET/$_POST, you can choose to look them up in $_SESSION. Or vice versa. Every time the user submits a form though, you should always update $_SESSION so that the values are the most current. (in case they backtrack, or resubmit, or whatnot).
session_start();
if (!empty($_POST)) {
$_SESSION['post'] = $_POST;
}
elseif (empty($_POST) && !empty($_SESSION['post'])) {
$_POST = $_SESSION['post'];
}
just don't forget to unset($_SESSION['post']); when you're done with it.
If I have understood your question correctly, I think you need some variables from database as well as from the session.
Simply put the key of your database tuple (or keys in case of multiple values) along with other stuff. Page, upon loading will check for the session variables and when it finds the key, it can use it to retrieve the data from the database.
In your previous page, the code will look like this :
$_SESSION["player_key"] = 56;
$_SESSION["tournament_key"] = 100;
And current page, start processing like this :
<?php
session_start();
$player = $_SESSION["player_key"];
$tournament = $_SESSION["tournament_key"];
/*
* your database connection steps
*/
$query = "select * from player where pid=".$player;
$res = mysql_query($query);
/*
Now assign values for your forms/components here or anywhere in the page. You don't have to read anything from the querystring.
*/
?>