Read the session data from session storage file
If you want to decode session data, use session_decode
(see the manual). unserialize
only decodes single variables, not session data.
You can do something like:
$file = '/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$contents = file_get_contents($file);
session_start();
session_decode($contents);
print_r($_SESSION);
Reading PHP's session data without loading it
I would start both sessions one by one and store $_SESSION value in local arrays.
i.e.
// Loading the first session.
session_name('first_session_name');
session_start();
// Now we have first session variables available in $_SESSION
$_FIRST_SESSION = $_SESSION;
// End current session.
session_write_close();
// Just to make sure nothing remains in the session.
unset($_SESSION);
// Now set the second session name.
session_name('second_session_name');
// Check and see if the second session name has a session id.
if (isset($_COOKIE['second_session_name']))
// There's already a session id for this name.
session_id($_COOKIE['second_session_name']);
else
// We need to generate a new session id as this is the first time.
session_id(sha1(mt_rand()));
session_start();
$_SECOND_SESSION = $_SESSION;
PHP Laravel: How to set or get Session Data?
Storing data
To store data, you can use:
Session::put('variableName', $value);
There is also another way, through the global helper:
session(['variableName' => $value]);
Getting data
To get the variable, you'd use:
Session::get('variableName');
Session Storage in js
Session storage is same as local storage but the only difference is that data stored in session storage will clear automatically once page session will expire.
Now come to your question. In your success
function, do like below to store data:
success : function(data) {
console.log(data.xhr.response);
sessionStorage.setItem('dataStored', data.xhr.response);
}
Usage is like below:
// Save data to sessionStorage
sessionStorage.setItem('dataStored', data.xhr.response);
// Get saved data from sessionStorage
var data = sessionStorage.getItem('dataStored');
// Remove saved data from sessionStorage
sessionStorage.removeItem('dataStored');
// Remove all saved data from sessionStorage
sessionStorage.clear();
For more information about sessionStorage
check this link : https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
Where is sessionStorage data stored?
It's not stored on the server, since we're still in client side when you're talking about Window.sessionStorage
. It's stored in a file called "sessionstore.jsonlz4". In any case, this question has an answer here.
How to store and read session (value) in AngularJs?
Your session storage code could look something like this using Javascript APIs for session storage. You will have to serialize your Javascript object as session storage only supports strings.
$scope.returnRefId = function (emp) {
try {
// test emp
alert(emp);
// session code here
sessionStorage.setItem("emp-key", JSON.stringify(emp));
}
catch (e) {
alert("some errror");
}
};
Alternatively, you can storage each property of emp
in a separate storage key. Debugging should be as simple as
sessionStorage.getItem("emp-key")
More information on session storage can be found here. https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
Also checkout ngStorage https://github.com/gsklee/ngStorage
Is it possible to load a variable from a JS script in another HTML file?
I can think of two easy ways.
1.) Try out sessionStorage: sessionStorage.setItem('key', variable);
will store the variable value in the browser session storage. sessionStorage.getItem('key');
will return the set value of said variable.
See: https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage
2.) Add the variable value to your url www.yourdomain.com?key=value
and retrieve it from your second script.
const urlString = window.location.href;
const url = new URL(urlString);
const value = url.searchParams.get("key");
See: How to get the value from the GET parameters?
Implement session storage in an Angular 8 application
For saving values while refreshing the page, you can use the localStorage
or the sessionStorage
for that. There is no external library or import necessary. It should work out of the box in most of the browsers.
For saving:
// clicks is the variable containing your value to save
localStorage.setItem('clickCounter', clicks);
// If you want to use the sessionStorage
// sessionStorage.setItem('clickCounter', clicks);
For loading:
const clicks = localStorage.getItem('clickCounter');
// If you want to use the sessionStorage
// const clicks = sessionStorage.getItem('clickCounter');
You can check this in Chrome using the dev tools.
How to store session data inside sessionStorage after invoking an API using PHP?
To use the SessionStorage on the browser you must use javascript that is a language executed on the client side (browser), php is a language that is going to be executed on the server side, so is not valid to do what you need.
To fix your example you can:
- Use php to call the API and send the result to the client assigned to one variable, so it can be read from javascript ans stored in the SessionStorage.
- Use javascript to perform the call to the API, using this way you are going to have less performance problems in the future.
I also recommend you to read something about client side/server side differences as was pointed by @jon-stirling.
Using your code, one example for case 1 could be:
$url_get_fc = 'http://apiurlhere';
//Send the request
$response_fc = curl_exec($ch_fc);
curl_close($ch_fc);
//Check for errors
if($response_fc === FALSE){
die(curl_error($ch_fc));
}
// Decode the response
$responseData_fc = json_decode($response_fc, TRUE);
// sessionStorage
echo '
<html><head>
<script type="text/javascript">
alert("'.$responseData_fc.'");
</script>
</head><body></body></html>';
Related Topics
Php-Sort Array Based on Another Array
How to Set for Specific Directory Open_Basedir
How to Convert an Image to Black and White in PHP
Check If String Contains Word in Array
How to Use File_Get_Contents() to Compare Two Files
How to Match Accented Characters with PHP Preg
Using If(!Empty) with Multiple Variables Not in an Array
PHP - Time Remaining Until Specific Time from Current Time of Page Load
PHP Soap Client That Understands Multi-Part Messages
Retrieving the Last Inserted Ids for Multiple Rows
MySQL PHP - Select Where Id = Array()
How to Make Codeigniter Accept "Query String" Urls