I know its a bit stupid question which is a bit meaningless but still I wanted to know is there any way to track last ajax call on page.
Actually the issue is I developed an application which works with ajax and many ajax calls happening with different different actions but if no ajax request call for 10 mins. it will expire session but I don't want it on just ajax call.
What I Want
if no activity happens in 10 mins. I don't wanna do anything but if even any click or keyboard activity happens then it will make 1 more ajax in after 10 mins. of click so session will not expire.
But that's not the issue to write script for it but the issue is I've lots of page and lots of different ajax call's so I just want to track last ajax call time on page.
I don't know if there's any possible way or not for it so I didn't write any code yet if any its possible then I'll start writing script otherwise I'll find another way but I appreciate if someone suggest me a way to do it.
Thanks
You can use ajaxsetup and increment a variable in ajaxsetup. Because ajaxsetup call in every ajax call
see the documentation of ajaxsetup http://api.jquery.com/jquery.ajaxsetup/
declare one variable and set 0 at the end of ajax request and change the value of variable as per time
i think this will help you
If you want your session not to expire, then increase its duration :
Adding this to your script will set php sessions lifetime to a month :
ini_set('session.gc_maxlifetime', 2678400);
ini_set('session.cookie_lifetime', 2678400);
EDIT for comment below :
To track last call add this at the beginning of your application entry point :
<?php
session_start();
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
$_SESSION['last_ajax_call'] = time();
}
Related
I'm trying to create a trading bot with PHP.
I would like to get the value of the currency and update it even without refreshing the page.
In order to do this I should execute this call every 5-10 seconds.
Is that correct?
$summ = $d->getMarketSummary("USDT-BTC");
Is there a way to do this asynchronously? Even if user doesn't reload the whole page.
I've heard of AJAX, but it's Javascript.
Thank you in advance.
You've heard it correctly, you need to do this in JavaScript, with AJAX. There are two parts for this:
1) You need to make an API in PHP, a route that will only respond with the data you want. So a page that when called:
<?php
$summ = $d->getMarketSummary("USDT-BTC");
echo $summ;
?>
And mapped to a url, let's say /data.
2) You need to make a JS in your page that calls that newly created route every-so-often; for that your need to use ajax (xmlhttprequest or Fetch API), and use the setInterval function to call it regularly and update the data in your page accordingly.
If you can't use javascript (AJAX) for this task, your only way is to create a CRON job that fires that PHP script every minute. Unfortunatelly, CRON jobs can't be configured to execute every X seconds, but you can fire it all minutes of the day.
I have an AJAX call that is running a long PHP script where it has 20 different results, I would like to show when each step in the script is done.
Like so 1/20 done, 2/20 done, 3/20 done.
Latest 29-12-2015 03:17.
I tried to create the JSON file as so (jsonFileName_uniqueTimeStampHere.json) by PHP, but the time taken to create the file with PHP, result in a 404 file not found error!
Because when the AJAX call is running it comes to the progress call before the file has been created, I know I can't create the file with JavaScript but is there anyway to create.
The file before the success callback from jQuery AJAX?
What would be the best way to show progress information while AJAX call is running.
The way I have it now, I have a JSON file saved on the server that gets updated with the latest state that has completed, but if multiple users is running the same script the JSON file with the state gets overwritten.
Should I save the state of each progress in DB and then receive it with multiple calls to a PHP method that get state that has been completed?
Should I keep using the current method I use and add a userID to the JSON file so it is unique on each call to the file?
How should I go about doing it the same way as Seositecheckup?
What is the best way to make a progress with AJAX and PHP?
Please tell me if you need any more information.
I have looked around and don't feel like the info or half of info, there is to find online has been enough to do this myself.
I would like to use jQuery AJAX and not XMLHttpRequest, I'm looking for something similar to seositecheckup.com, when you scan a page you can see the state update on each completed function in the console and is done with different AJAX calls. How is that possible?
Should I forget about using jQuery and keep focus on plain JavaScript instead?
Right now I have a setup with jQuery that works the problem is, that I use a JSON file to get the result from and it gets overwritten when multiple users request the same script, is it possible to store the state in db instead and receive it from there with some unique identifier?
In the future I would like to make it possible to put the script into a queue that could be run and when the script ends it should send an e-mail to the user.
The HTTP way of handling requests that may take a long time is for requests to return a 202 and the body of the response should contain the URL where the user can query for the result.
#Request
POST /some/entitities
...
#Response
HTTP/1.0 202 Accepted
/jobs/{jobId}
The user can then poll /jobs/{jobId} which can return a number to represent progress. Do you have to use this? No, but if you do, others developers can immediately recognize what is going on.
Even if you don't use the approach I recommend, you will also have to keep track of job progress in your database and have a separate AJAX call to find out the current progress.
If I make an AJAX $.post call (with jQuery) to a php file for updating a certain parameter/number, does it considered bad practise, dangerous or similar?
$.post(file.php, {var:var}, function(data){
// something
}, json);
It would be a single user on a single page updating a number by clicking on an object. For example if user A is updating a certain number by clicking on an object user B should see this update immediately without reloading the page.
It depends on 3 main factors:
How many users will you have at any given time?
How much data is being sent per request on average?
Given 1 and 2, is your sever set up to handle that kind of action?
I have a webapp that's set up to handle up to 10-20k users simultaneously, makes a request each time the user changes a value on their page (could be more than 1 req per second), and it sends roughly 1000 bytes on each request. I get an average of 10ms response time, however that's with node js. Originally I started the project in PHP but it turned out to be too slow for my needs.
I don't think web-sockets is the right tool for what you're doing, since you don't need the server to send to the client, and a constant connection can be much more expensive than sending a request every few seconds.
Just be sure to do lots of testing and then you can make judgements on whether it'll work out or not for your specific needs.
tl;dr - It's not a good idea if your server can't handle it. Otherwise, there's nothing wrong with it.
Another solution could be, to cache user actions in local storage/variables, and send them all at once every 10-15 seconds or so, then clear the cache, when sending was successful.
In this case you should also validate the data in local storage to prevent tampering.
I have to build a results viewing page where I have to list results of a voting process in live. I am planning to check for DB changes in every 2 seconds ? Is it doable ? will the page crash or get stuck after 1 or 2 hours?
I think you can use settimeout to call the ajax function.
The second better way would be if its possible in your existing architectural setup, use setimeout for making an io connection and then checking for any event , this event is nothing else but a change in the db which is triggered by a node js server.
HI guys, I need to be able to make an ajax call to be made after every few minutes. Basically the ajax call would be to check on new emails in an inbox. If there are new emails it would download the emails to a database. I got all the server side code set up fine. I just need to know how do I set up on the front end the part where the ajax call is made after every few minutes plus it should be set up such that we don't end up with parallel ajax calls being made i.e if the ajax call hasn't returned a response it shouldn't start a new ajax request.
http://www.w3schools.com/jsref/met_win_settimeout.asp
or
http://www.w3schools.com/jsref/met_win_setinterval.asp
set interval is probably more like what you after, you can then call something like
setInterval(function(){ /*code you want to run here */ }, 100);
then your code will run every 100 milliseconds.
You will obviusly need to use a much larger time, something like 60000 for every minute. If you use the settimeout method then you can have it check wait for the response of the ajax, and let the code the handles the ajax response set a new timeout.
There is also Prototype periodical update that makes things a lot easier
few examples
http://www.tutorialspoint.com/prototype/prototype_ajax_periodicalupdater.htm
http://www.prototypejs.org/learn/introduction-to-ajax
http://www.prototypejs.org/api/ajax/periodicalUpdater
I would set up a cookie that bounces back and forth between the server and the client to store the last time an ajax request was made. Then on each page load, a js script reads the cookie and sets a setTimeout for the remaining time before the next ajax request.