How to add values to a php session with jquery - php

I have an audio player and I want to send the time when I access another link, so when it's loaded it can continue from the exact time I pass in the session
any help?

Why would you need to use PHP sessions for this?
I'd use a cookie, or if older browsers are'nt an issue, local storage, or maybe a script for using cookies on older browsers and local storage on newer browsers.
There's a handy cookie plugin for jQuery.
Local storage is pretty straight forward aswell:
localStorage.setItem('timeIleft', time_variable);
and to retrieve:
var user_left = localStorage.getItem('timeIleft');

You need to call an AJAX requets to do that.
The URL will be a php page and will retrieve your data using $_POST['param-name'].
Or, check this way to call php page http://www.xajaxproject.org/

Related

Cannot save cookies or use a db, need to display a message only once

I'm developing for a pre built php based system.
For restrains that I cannot explain, I cannot use any cookies, sessions or a db link.
I need to display a message only once to every visitor that comes in.
Any creative ideas you guys can think of?
Thanks,
Here are some ideas
Use browser's localStorage
Pass the message using post or on querystring. Not safe option
Use flash store. Same as #1 but don't need flash!
Use file disk but then you need to be able to remember who owns the file since multiple people would be on the same server. Without using cookies, I am not sure how to do this.
I personally like using localStorage. First page would create some javascript that stores a message on browser's storage and then second request would pull that message and display it.
While your restraints are somewhat curious I suppose you can add a value in your $_GET.
If a visitor visits www.yoursite.com/ they will see your message, all links displayed on that page include a $_GET var 'displayed' or however you wish to name it. so links will be like www.yoursite.com/page.html?displayed=1
Then you check
if (isset($_GET["displayed"]))
// don't display message
else
// display message
Not a very 'nice' approach, but it would work.
What about using IP Tracking and a flat file or csv. Something similar to that will display it only once per IP.
You can force PHP to pre-pend some of your own PHP code, including a session_start() call, with the auto_prepend_file .ini option.
This guarantees that whatever file you prepend is THE first thing executed in each script.
After that point, you've got a session (or even just a cookie) in which you can store your flags to hide that message on subsequent visits.

Equivalent of PHP Sessions in JavaScript

I was going to ask about how to implement sessions in JS,I found few functions that can be used like this example I found :
String exforsys = request.getParameter("test");
session.setAttribute("test", exforsys);
And then just use session.getAttribute( exforsys);
But no response, I guess it's only used in servlets or something.
Anyway, I decided that maybe someone has an alternative way other than just use sessions, what am trying to do is click on a link in one page and depending on which link was pressed I will load different information.
Since its a onclick function, I'm stuck with JS!
So I need to pass this information to the second page, cookies works well cause it can be handled with both PHP and JS easily but some computers deletes cookies and that wouldn't be nice!
Any suggestions or other ways I can reach what I want but with out using sessions?
Sessions are server variables. Thus cannot be used by JavaScript.
However, you can retrieve the session variables, through ajax request.
Script (jQuery)
//This portion will be triggered once the DOM is loaded and is ready
$(document).ready(function() {
$.post("getsession.php",
{ "variable" : "yourneededsessionvariable" },
function(data) {
//data contains your session data
}
);
});
PHP
//getsession.php
<?PHP
session_start();
echo $_SESSION[$_POST['variable']];
?>
Use local storage or client controlled cookies.. Sessions uses server-controlled cookies. Cookies are just small files that resided on the client.
A session handle is stored in a cookie. If cookies are not accepted, the server will add the sessionID to the URL. If you do not have cookies, you cannot persist anything except in the url.
Why does "onclick" stop you from using sessions? You can ajax things to the server and add them to the session onclick
A session generally means "Some data stored on the server and associated with a user via a token stored in a cookie". You can't do that with client side JavaScript (for obvious reasons).
You could store data directly in a cookie.
If you are willing to sacrifice wide browser support, then you can get increased storage by using one of the client side storage mechanisms introduced by HTML 5 and Friends.
maybe someone has an alternative way other than just use sessions,what am trying to do is click on a link in one page,and depending on which link was pressed I will load different information.
Just link to different pages.
some computers deletes cookies and that wouldn't be nice
If they delete all cookies, then a session isn't going to work either.
Append the data you want the next page to get on the query string.
123
456
Then on foo.html you can inspect location.href to see what was passed in. THere is no need for cookies here.
PHP is a server scipring language while javascript is client end language
you cannot literally make sessions in javascript
Why not just use request parameters? i.e. http://yourserver.com/page.php?link=1

Validating user authentication for all page

I am using the HTML page on front end PHP for server side scripting. now the problem i have configuration page which can be loaded by disabling javascript on Mozilla setting.
Since i am using cookie to validate on the javascript
For avoiding this i need some thing i can call some php script onload without intimating user and verification return page
regards
hemant
you can not call PHP onload (at least not without ajax, but then you're back in the javascript boat).
onload is a client side event and PHP runs on the server...
I can't really see what you're doing but my giess is:
what you want to do is create a PHP script which catches all requests and renders the correct page if the person is autorized. or something like that.
You could use a framework (such as using MVC etc.), even a really tiny one, and then authenticate your requests when you initialise your Controller.
Or if you want to custom code:
use rewriting (.htaccess on Apache) to capture all your requests and route them through a PHP file (e.g. index.php)
this file will authenticate the user and then explode the route requested (GET) to send them to their destination:
$route = $_GET['route'];
Once you have the user authenticated successfully, store that information in $_SESSION array.
More info:
PHP Sessions Tutorial
PHP Sessions (php.net)
Couldn't you just do a check if there browser has JavaScript enabled or disabled?
then use PHP session?
Moving away javascript based cookies, and possibly using php session to validate the
login, or using php cookie.. that way the sites not dependent on the javascript,
especially if its a security issue..
<script type=”text/javascript”>
document.write('Javascript Loaded!');
</script>
<noscript>
<div style=”color:#FF0000; font-weight:bold; text-align:center;”>
Javascript must be enabled to see this site!
</div>
<meta http-equiv=”refresh” content=”0; url=http://www.yourdomain.com/nojs.php”>
</noscript>
this way you could redirect them to your php page for processing..?
I'm not sure what exactly it is what you're asking but if you want to secure a site, and you don't mind if there's just one login/password combination, you could also use .htaccess.

How can I get Flash to share the browser cookies/session?

I'm building a PHP-based web app and am integrating a Flash-based charting engine. The Flash chart needs to make a AJAX request for its data. This request fails because it is seen as a new user agent and doesn't contain the PHP session cookie to identify it. So, it gets redirected to the login page.
I've read a few hacks to make this work, including supplying the session ID on the querystring, but that opens up security holes. How can I get Flash and PHP to share cookie-based session state automatically and stay secure?
In IE it will work naively. In firefox, the only way to achieve this is to POST the session id into the flash script (the php processor that is), and have it restore the session from that.
If the session cookie is initiated early enough, then it should be OK. I've had a similar problem with cookies shared between JavaScript AJAX and Flash requests (if you want to call that AJAX too, go ahead :-) ), and we solved them by making sure the JavaSCript finished the request that initiated the cookie early enough so that when the Flash sent the request, the browser already had the session cookie.
Also making sure the cookie path was set to "/" was a good idea.
That being said, if you can't get it to work - as dirkgently said - you can store the information in the HTML DOM using a JavaScript AJAX call, and then fetch it from the Flash object using an ExternalInterface call. But do make sure to set at least "allowScriptAccess=sameDomain" on your Flash object
You should be aware that transmitting a session ID in a Cookie: header, or in the argument field of the GET HTTP directive is of no different security.
Use ExternalInterface to talk to the Flex chart. Some browser related information can be passed around via the LoaderContext and BrowserManager classes as well. Dig in a bit into the AS3 documentation.
you can try and send to php 2 parameters one session_id and a second one that is an key that combines some information from the client ( ex ip ) and encrypt it with a key stored on the server and on the request from flash you check to see the second paramaters matches the client request, this way if somebody trys to do a session stealing they cant because they will not match the second param

How to reset state of DataTable defined with filters or/and search field, with PHP

Working on PHP application, that uses DataTables (https://datatables.net) on several layouts.
Can I somehow reset all DataTable's filters and search data, after end user logged out from application? In other words, to clear all cookies that DataTables library created, if it's possible with PHP functions..
Main idea is to reset application to it's main state after user is logged out from application.
Thank you in advance!
The documentation says: (https://datatables.net/examples/basic_init/state_save.html)
The built in state saving method uses the HTML5 localStorage and
sessionStorage APIs for efficient storage of the data.
It means you cannot reach it with PHP.
However you can change the storage of filter parameters to cookie or server side.
Alternative options of using cookies or saving the state on the server
hrough Ajax can be used through the stateSaveCallback and
stateLoadCallback options.
You should write code which:
- save the filter parameters to cookie what you can remove on logout with PHP
or
- save the filter parameters to database via AJAX and you also can remove it on logout with PHP
example is here:
https://datatables.net/reference/option/stateSaveCallback
In case anyone out there is interested in a solution to this:
As some people rightly pointed out, there's no way to delete localStorage from PHP because PHP works only on the server and localStorage is client-based.
However, a way to achieve what OP wants is make sure that the page shown after logout has the JS needed to delete localStorage. For example, say that after logout, the user gets redirected to comebacksoon.php; what you need is to make sure that in the html of this comebacksoon page, you include:
<script>
localStorage.removeItem(keyYouWantToDelete);
</script>
It should be noted that if the user closes the window before comebacksoon.php is loaded, localStorage won't be deleted, but this approach has worked for me in most cases.

Categories