I have implemented auto refresh function on a webpage which refresh page after 2 second. Initially I wanted to do this using shall script which will hit that url in every 2 second. but server administrator advised me that it is not good to hit a page in every 2 second because it create extra load to server. Now I want to know if autorefresh also create the extra load to server same as shall script.
Thanks
Related
I am trying for a few days now to figure out how I can do this.
I have two pages, one is index.php, other is add.php .
1)Index.php - I want it do display last 5 rows from database table.
2)Add.php - contains form which adds new row to database.
How can I update values that are shown on index.php when I add new row?
So lets say I have index.php already opened on one tab, and on other one is Add.php. When I fill the form on add.php,
I want it to automatically notifies index.php that there is a new row,and that it should change values without refreshing the index.php tab.
Do I need VPS for this?What is the best way to do this theoretically?
There is no need in setting up a VPS for this. A VPS is Just a server in which u can install your own software. You are already using a server for PHP (local perhaps).
U can create a websocket server. Once you add a row you send a message tot the websocket server which will then tell the index page to resfresh.
Previous option is a lot of work for a rather simple functionaliteit. I'd suggest the simplest option as recommended by FrankerZ. Only if you refresh the page at a set interval using JavaScript (window.setInterval) then the page would keep refreshing. I don' t think that's ideal. You van however send a xhtmlhttprequest to store the contents for the index page and run that request every x seconds. Then u can compare the 2 contents and decide if the page should be refreshed or not.
I have an application where I use ajax to update the comments & Like count.
I am using Codeigniter page caching http://www.codeigniter.com/user_guide/general/caching.html
and set following code to recreate caching every 60 minutes
$this->output->cache(60);
Problem is when somebody enter a new comment, DB operation is happening (Because of Ajax call), But the Newly inserted comment disappears after the page refresh, because of the cached HTML page. How to handle caching and also dynamic content to change?
The main purpose of caching is to save server resources on page load (i.e. so the server doesn't need to fetch all the dynamic data every time its loaded).
It sounds like you are using the AJAX function to both submit the data but also modify the webpage on the client side simultaneously. When the user then refreshes the page, this will fetch the content back from the server, in this case the original cached content that was generated before the comment was made and is therefore operating exactly as it should.
If you have a true requirement for caching this page, you could consider deleting the cache, which would force it to be rebuilt on the next page load whenever a comment is made. Placing the following line of code in your controller (in the function that received the AJAX data) should do it:
$this->output->delete_cache('foo/bar');
I found the solution.
To delete a cache file you need to use the following function
$this->output->delete_cache('CONTROLLER/FUNCTION');
Note: No Slash before or after.
In my Case i was using custom routes in codeigniter. When user type example.com it is routed to example.com/CONTROLLER/FUNCTIONNAME and that too default landing page.
So i created a new function. Please refer here
I have a webpage that is auto-refreshed every 240 seconds with the generic HTML Meta tag. During the refresh it pulls data from a database which takes the site about 15 to 20 seconds to build before it's shown to the user. During this time I wish to show a small DIV with a loading message telling the user that it's loading data.
The more complicated thing about this is that the user has a few menu options to filter out specific data from the database. When clicking such an option the page is reloaded again and takes 15 to 20 seconds to build.
Users that aren't familiar with this loading time might feel the need to click the same menu option over and over again within a few seconds hoping that the page will load faster. But instead it will most likely cause the database server to get overloaded with requests.
So, to tackle this I wish to use jQuery to show a loading message, then have it load the data from the database (with a PHP script) and finally dump the data on the page.
I've done something similar but that was limited to users clicking a link which caused a jQuery script to load the data while showing the waiting DIV (using CSS rules).
I can't figure out how to implement this solution for an auto-refresh.
Some help would be nice.
You can use the same solution with auto-refresh as well, with the mention that the initial page load doesn't container the data that requires the DB call, but instead shows a loading message and starts an AJAX call to a server side script that returns the data.
Your page load:
Request
Server query DB
DB Response
Page loads (with data)
Ideal page load:
Request
Page loads (without data) <- loading message here
AJAX call
Server query DB
DB Response
Page updates (with data)
I'd second megawac's comment. Don't use a meta refresh. Also, 15-20 seconds is a very long time for generating a database report that is going to be generated every 4 minutes; odds are that you're bogging down your server pretty badly. Very few queries should really take that long, especially queries that need to be run nearly continually. I would strongly recommend refactoring your queries or doing some caching to speed things up. If you post some code, I'm sure people would be happy to look at it.
I have a simple web page that has 3 tabs in my main content area. Once a user clicks on a particular tab, I use jquery and ajax to fetch some data from server and (of course) without reloading everything I change some div below those three tabs.
I use $.post to get data from the server and there are 3 different php files on a server that I call (one for each tab) and get data from.
Is it possible to start a php session when a user clicks on the first tab and save some $_SESSION variables and then use them when a user clicks on the second or third tab so that I know what user clicked on the second or third tab?
Thanks!
Newman
Yes, the sessions are shared across all php scripts. So whatever you save in the first tab will be readable on 2nd and 3rd tab. Keep in mind that
Edit:
Thing to keep in mind here is that the domain should remain the same for all scripts to be able to see the session.
I'm trying to find a solution to this problem: I have a page on a php application that can't be opened more than once to avoid session overwrite.
I'm spending a lot of time without finding any solution!
The last one I tried is to use the jquery "load" and "unload" events on the window. It works fine if I open the same page in a new window or a new tab but it also blocks the page if I refresh it!
I really don't know how to go on! I thought this was a simple example of semaphore usage...but I can't code it in the right way.
Any suggestions? (both php and js solutions are welcome)
You could try with a cookie:
The first page load, the cookie is set(by the server for the session)
The next time the same page is loaded, it reads the cookie
If the cookie is there, do not overwrite session variables
I don't think the browser will be able to make a difference between a refresh and a load in another tab/window.
Can't be opened more than once by the same user, I presume.
How did you use jquery's load and unload events? Brainstorming here, how about using load to set in the user's session a semaphore, which is removed by unload. Ofcourse I'm not sure if that event fires even when the browser crashes or similar unforseen technical issue. In that case you might have a stuck one that you'd need a specific timeout for, or to wait for the session to time out.
To fix that perhaps after the page loads have a timer that executes every 10 seconds, basically updating the semaphore sort of like "The page is still open", along with current timestamp. If on load the semaphore exists but the timestamp is older than 10 seconds, then the user is refreshing.
The problem is that you do not have access to the end user's pages that are currently open in a browser. So the only solution is for you to keep tracking the pages the user opened/closed/switched to another page/etc. Even that isn't perfect as the user can open another browser process. So there is no perfect solution to this.
sorry guys...you're right!
A bit of code will help all of us :)
suppose my script is script.php
in the html of script.php i put
$(window).bind('load',
function(){
$.post(PATHTOLOCK.php);
});
$(window).bind('unload',
function(){
$.post(PATHTOUNLOCK.php);
});
In the file PATHTOLOCK.php i do this:
$_SESSION['flag']=true;
And in the file PATHTOUNLOCK.php i do this:
$_SESSION['flag']=false;
At the beginning of script.php i put
if($_SESSION['flag']==true){
echo "error";exit;
}
If I open script.php in two windows/tabs everything works fine. If I refresh the page it doesn't work because I suppose the events sequence is the following:
click on refresh
unload event is not invoked because the page is not completely left
the script.php is reloading
the check at the beginning of script.php fails because flag=true
script.php goes in error
the unload event is invoked because the page is left and flag=false
click on refresh
now it works!
and so on...every two refresh it works!