use session cookies per specific browsertab - php

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.

Related

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">

passing varables' value from one form to another in php

I have three forms - payment.php , payment1.php and paydb.php . payment.php contains the front end form.payment1.php contains the back end of the form of payment.php. whereas we are shifting to paydb.php from payment1.php. Now I'm filling the form by entering member number in payment.php which is retrieved in a variable $member_no in payment1.php .Now I want to get the value of member_no in paydb.php . How to do that ?
After receiving $member_no in payment1.php redirect to paybd.php with a get array
using
header('Location: http://www.example.com/paydb.php?member_no=$member_no');
then receive $_GET['member_no'] number and assign to a variable
example:
$member_no = $_GET['member_no']
The first thing is make sure you are not passing sensitive information where the public can see it. Such as in a URL.
As soon as you get the member's number... store it in a session variable.
You can probably do this when they log in.
session_start();
$_SESSION['member_no'] = $member_no;
OR on the first payment page (assuming 'member_no' is the name of the form element being passed) like this...
$_SESSION['member_no'] = $_POST['member_no'];
Now that session will persist as long as the visitor has their browser open and you don't have to worry about passing it from page to page.
You can use that session on any subsequent page simply by calling it.
<?php echo $_SESSION['member_no'] ?>
Without showing this information to the public.
ALWAYS make sure you place this at the top of any page where you want to use session variables.
if (!isset($_SESSION)) {
session_start();
}

php session always created on refresh

I am new to PHP so have a very basic question
I creating a page I am creating a page initially with user id and password, once user id and password are entered and submit is clicked, AJAX is called to validate that against database.
once validation done I want to refresh the page which show more option to user
I was thinking to use session
but every time I refresh the page a new session is created
I put this at the top of the page as a test and always when F5 is press I see "new session" on top of the page
<?php
if (!isset($_SESSION)){
session_start();
echo("new session");
}
else
{
echo("old session");
}
?>
session_start must be called always before anything related to a session. After calling it, you can get or set values of the $_SESSION variable.
Reference.
Your code should be:
<?php
session_start(); // always call this at top
if (!isset($_SESSION['has_been_here'])){
$_SESSION['has_been_here'] = true;
echo("new session");
}
else
{
echo("already been here");
}
?>
From php.net:
session_start — Start new or resume existing session
That means you have to start your session with
session_start();
on every page, in the first line, which will start or resume it. Check the php.net manual, it will help you understand how to handle and check sessions correctly.

PHP+AJAX (jQuery) - Reading session data on $.post() return

I am currently writing an "Edit Account" module. Using jQuery.post(); I can successfully update my database, and I can write to the session variables from the PHP script - however,
when trying to access the Session variable that I just wrote to, the data does not seem to have been updated.
I write to the session like this: $_SESSION['email'] = 'john#doe.com'; in my Ajax.php
My jQuery (minified) callback on success:
$.post(...,function(res)
{
if(res=='success')
{
// Alert the new E-Mail, read from the Session!
alert("<?php echo $_SESSION['email']; ?>");
}
}
However, the alert does not output john#doe.com, it outputs whatever the E-Mail was it was when the entire page was loaded. When I refresh the page, the session data has been properly updated.
I do have session_start(); on both index.php (my site) and in Ajax.php
If you need more info please do let me know. :)
First of all it should be
alert("<?php echo $_SESSION['email']; ?>");
and secondly $.post is dynamic but <?php echo $_SESSION['email']; ?> is static. It renders once, on load, not everytime you make an .post() so if
$_SESSION['email'] = 'blah'; by the time the page is loaded, the alert will always be alert("blah"); , until you reload the page.
If you want to alert the new session variable you have to make a new ajax request to a php file (ie. return_session.php?key=email ), that will return the new session variable
Thats because the alert string is generated when the page is loaded. And when you make an ajax request it still doesnt change, because its not refreshed.
On ajax success you should output the responseText and then it will work.

jQuery AJAX and PHP session issue

Really hope this makes sense and someone can point me in right direction. On a registration page (parent page), when you enter a license code a jQuery ajax success function loads content from another page with an appended url to include a session id:
success: function(data) {
if (data=="RegisterError") {
$("#error").show();
$("#processing").hide();
} else {
$("#contain").load("download-software.php?sessionid=xXXX #req");
}
}
The page "download-software" has the following PHP referrer check to make sure the content is being requested from the registration page via the session ID and redirects you if it's not:
<?php
$code = $_GET['sessionid'];
if(strcmp( $code , 'xXXX' ) != 0) {
header("Location: http://www.someotherpage.com");
}
?>
That works fine. Now what I need to do is in the "download-software #req" content that is loaded into the parent page, have a link that when clicked replaces the "download-software #req" content which has been loaded inside the parent page with content from another page and do the same type of session id check.
I cannot get it to work. I place the following code on the "download-software #req" content for the
<a id="beta">beta notes
$("#beta").click(function() {
$("#req").load("NT7-SP1-Download.php #betaNotes");
});`
I've also tried using the .live function. How do I start a new session and make this work?
*answer**
I used the live function on the parent page and it works fine. Making it too hard i guess.
I would refrain from using GET query string parameters and use the included PHP session functions.
use:
<?php
// initialize the session
session_start();
// assignment call
$_SESSION['key'] = 'value';
?>
You can retrieve data from the session on the same server in subsequent page requests.
When you are done with the session, use: session_destroy()

Categories