I have a wordpress site. In my site there I have this one specific page that shows a lot of content. This content is based on many custom post types. I have built the page by writing a specific php file for that page called patio; i.e. page-patio.php.
The problem is that since the logic is complex it takes the server about 30 seconds to respond, I have optimized images and everything else that loads at the time; but I see that what takes too long is the server response.
I could try to optimize at server level, but I am seeing that it does not make any sense that all that complex logic and database reading should by done every time a user wants to display the page. The data changes once a day, maybe more often in the future.
I want to run a cron that executes a php snippet. Then that PHP snippet would prepare the page, i.e. write the html. So when a user clicks on the page I should just show that static html page and with javascript let him navigate the content.
Anyone found a solution for this?
Good morning.
Is it a server problem, or number of CSS, JS and cookies? Is it a shared server?
Maybe you change your Theme for one that use pure javascript, or a few libraries JS and CSS be better. Be right about problem: if it's with server, or number of libraries and cookies, or both situation. However, if you can't change anything, you can contract a better server service for your application.
I gave up using wordpress to priorize page performance.
I was able to solve my issue very nicely, thanks to this 10 year old post:
Link to Stackoverflow post on saving to html page
I created a cron event that runs periodically that runs a PHP Snippet; I use the Code Snippet plugin to create the event:
if ( ! wp_next_scheduled( 'iKid_cron_twicedaily' ) ) {
wp_schedule_event( time(), 'twicedaily', 'iKid_cron_twicedaily' );
}
add_action('iKid_cron_twicedaily','iKid_Static');
And then the same plugin to create the iKid_Static function.
This function uses the ob_start() and file_put_contents('yourpage.html', ob_get_contents()) commands to write the html page on my server.
And then on my actual page I code:
$content = file_get_contents($cacheFile);
echo $content;
This way my users now get all the information on that page in just 2 seconds, down from about 30 seconds.
I will be able to improve this surely when I move to a better server, but for now that is a great improvement for me.
Related
I'm using the simple html dom class and have gotten it to work on basic pages and can view the information I want. However, when I attempt to use it on a page that reloads a div with ajax, I can't seem to get it to "wait" before reading the page.
I basically want it to load the page, then wait 2 seconds before reading the page content (so that the new div has time to load). Is this possible or am I trying to use the class incorrectly? I'm manually inputting the URL, so it's not a link issue.
Example Page:
- You can see the load issue when you navigate through the pages.
Someone suggested curl and I tried that with the same results.
Thanks in advance.
PHP runs on the server. JavaScript (e.g. AJAX) runs in the browser, after the PHP code on the server has finished producing the page. You can't make a PHP program, running on the server, wait for an event that happens later in the browser.
You'll need to either load the content for that div using PHP code, or replace the PHP DOM-parsing code with JavaScript code that does the work on the client.
You can use the Sleep method ( http://php.net/manual/en/function.sleep.php ) if you simply want to delay the program execution for some set amount of time.
To load page elements asynchronously I use jquery load() and it greatly improves user experience. But the crowlers do not know anything about javascript, so site ranking should drop. To prevent it I created sitemap page. I do not see other solution.
The bad thing is that url is different. Normal page is page.html and sitemap's one is page2.html. This means that crowlers will have page2.html indexed. So, users would come to page2.html. But if they start browsing the site they'll come to nice pages with asynchronous load.
Yet, I am not absolutely sure that I have chosen good strategy. Can anybody say what seo problems can happen?
UPD: I've got solution! I simply try to place a cookie with javascript and if on next page load php scripts finds it, then it performs asynchronous load. Here's example:
<script type="text/javascript">document.cookie = 'checkjs=on';</script>
And then:
if (!$_COOKIE['checkjs'] || $_COOKIE['checkjs']!='on'){echo 'js is off, hello Google!'; } else {echo 'js is on, can use jquery load';}
That's bad solution - if you have important async load ( eg. page content - your case ) you must make sure that client withut javascript can get anywhere on your site ( this is done automatically by most MVC frameworks ).
Note 1 - any ajax page changes must be done via a onclick attribute ( so if javascript is off, classic href will do the trick )
Note 2 - if anyone don't opens main page ( page.html ) - he must get same layout as if he clicked thru your site there
If content is only made available through JavaScript then it is not search engine friendly. It also isn't accessible which is not a coincidence. To properly display that content so everyone can find the content you need to make it all work without JavaScript then go back and use JavaScript to enhance the user experience. This is called progressive enhancement and is the proper way to build a public-facing website.
I have a page where it shows users posts and refreshes automatically using the jQuery setInterval function.
$(document).ready(function(){
setInterval(function() {
$('#content').load('test.php');
}, 5000);
});
But the problem is I am going to have to create a duplicate page called test.php containing the same content which will be called every 5 seconds. I don't want people just viewing the source and finding the page with all the data on.
For example this site has a recent forum topics page which updates every couple of seconds,
http://awesomescreenshot.com/0d4o0n2e0
I look in the page source and find the link to the page and this is what I find
http://awesomescreenshot.com/0a2o0n691
I don't want people to be able to find that...
Is there a better way round this jQuery function? E.g. calling a php function to just run the query which will be in the test.php file?
Thinking about security by thinking where the data is going isn't quite right. Instead think about who has access to it. If you don't serve that data from the PHP to someone who shouldn't see it in the first place, then it doesn't really matter how they view it.
So your test.php needs to have security around it that hooks into your authentication. In psuedocode:
if (current user is authorized)
send data
else
403 Access Forbidden
Security through obscurity will only hurt you in the long run. Even if you could obscure the location of that data, it leaves open the possibility that someone may find it eventually. So do the security on the backend, out of reach of hackers, instead.
I have a site that get content from other sites with some JSON and XML API. To prevent loading problems and problems with limitations I do the following:
PHP - Show the cached content with PHP, if any.
PHP - If never cached content, show an empty error page and return 404. (The second time the page loads it will be fine "success 200")
Ajax - If a date field does not exist in the database, or current date is earlier than the stored date, load/add content from API. Add a future date to the database. (This makes the page load fast and the Ajax caches the content AFTER the page is loaded).
I use Ajax just to run the PHP-file. I get the content with PHP.
Questions
Because I cache the content AFTER it was loaded the user will see the old content. Which is the best way to show the NEW content to the user. I'm thinking automatically with Javascript reload the page or message-nag. Other prefered ways?
If I use very many API:s the Ajax loadtime will be long and it's a bigger risk that some error will accur. Is there a clever way of splitting the load?
The second question is the important one.
Because I cache the content AFTER it
was loaded the user will see the old
content. Which is the best way to show
the NEW content to the user. I'm
thinking automatically with Javascript
reload the page or message-nag. Other
prefered ways?
I don't think you should reload the page via javascript, but just use Jquery's .load(). This way new content is inserted in the DOM without reloading the entire page. Maybe you highlight the newly inserted content be adding some CSS via addClass().
If I use very many API:s the Ajax
loadtime will be long and it's a
bigger risk that some error will
accur. Is there a clever way of
splitting the load?
You should not be splitting content in first place. You should try to minimize number of HTTP requests. If possible you should be doing all the API calling offline using some sort of message queue like for example beanstalkd, redis. Also cache the data inside in-memory database like for example redis. You can have a free instance of redis available thanks to http://redistogo.com. To connect to redistogo you should probably use predis
Why not use the following structure:
AJAX load content.php
And in content.php
check if content is loaded. yes > check if date is new. yes > return content
there is content, but its older > reload content from external > return content
there is no content > reload content from external > return content.
And for your second question. It depends on how often the content of the api's needs to be refreshed. If its daily you could run a script at night (or when there are the littlest people active) to get all new content and then during the day present that content. This way you minimize the calls to external resources during peak hours.
If you have access to multiple servers, the clever way is splitting the load. have each server handle a part of the requests.
Can I Stop or interrupt PHP script for particular time?
I would like to implement logic like below:
on mydomain.com/index.php there will be flash appear showing some intro of a product for suppose 20 sec. Once it complete, on same index.php page the home page of site will appear.
I am not aware about flash (action script).
Is it possible in PHP or Javascript ?
Usually "splash pages", as the're called, are made up of a seperate page.
In flash you can use the following code (Actionscript 3). Put it int the last frame, or use an event listener to redirecrect when the file is finished. The actual redirect looks like this:
getURL("http://www.woursecondpagehere.com", "_self")
Where you place it is up to you.
EDIT: I think that this is a reliable solution because this guarantees (if implemented correctly) that the page won't move until Flash is done. CSS and Javascript will work fine too.
There isn't a need to interrupt PHP in the scenario given. Though I think what you want is to load the rest of the HTML after a certain event occurs.
If thats the case then you can use AJAX to load the additional HTML from the server. Or you can use CSS to hide that content and show it after a certain point.
The META Refresh tag is probably not what you want since it will redirect the user after 20 seconds, regardless of how long it took to load your Flash file, then play it. Since the speed of the user's connection cannot be reliably predicted, you will probably end up with a poor user experience.
What you want to do is definitely possible but it will involve some interaction between the Flash object and the rest of your page. If you could do as Moshe suggested and simply have the Flash object redirect the user's browser to your actual home page with content on it, that would be easier.
If you insist on keeping everything on the same page, one way to do it is to call a Javascript function from the Flash object once it's finished playing. The function you call should be written to hide the Flash object and/or it's container and display the container () with all of your content that you're ready to show.
See this Codefidelity blog post for a quick tutorial on how to call JS functions from Flash.
Also, to clarify, you won't be interrupting or changing when your PHP script runs. That runs on the server before the page is created and sent back to the user's browser. All you need to do is structure the HTML/CSS of your page to have two DIVs: one with the Flash object in it and the other with all your normal page content. However, set the CSS to only show the DIV with the Flash object, then finally use Javascript to hide that DIV and show the one with the content in it.
Try this,
write the your flash (splash screen) <embede> code in index.html and simply use javascript redirect function with javascript timer functions to redirect to index.php where you actual content is there.
something like...
window.location = "http://your.domain.name/index.php"
or
location.href = "http://your.domain.name/index.php"
use setTimeout() to call redirect after specified time.