WordPress - Load PHP variable so it's not cached (AJAX?) - php

I have some PHP code in WordPress (running in the Shortcode Exec PHP plugin) that runs, and loads a variable and displays it in the page.
I have a caching plugin on the site (recently switched to WP-Rocket), and when the plugin is enabled on the whole site, that variable is cached as well, so it doesn't update every time the value changes and the page is reloaded.
How can I load the code/variable with AJAX (or some other method) so that it isn't cached?
Here is the PHP code I'm currently using:
include '/full/path/variables.php';
$countsrc = $totalcount;
$count = (int) $countsrc;
switch ($count)
{
case 0:
echo "There currently aren't any items in the list.";
break;
case 1:
echo "There is currently $count item in the list.";
break;
default:
echo "There are currently $count items in the list.";
}
Is there any way to use some method like Ajax to make sure that the variable isn't cached?
Any help/input would be really appreciated!
Thanks a lot!

Related

How to continue controller execution after load->view in CodeIgniter?

I have a form to add new product. When product is added I need to write notifications in database for users that would be interested in this product. What I want to do is to display success message and continue with saving notifications.
In plain php we would use combination of ob_start(), ob_end_flush() and ignore_user_abort() however I can not get to understand how to get this working in CodeIgniter.
Here is simple example:
if ($this->form_validation->run()){
$this->load->view('success', $data);
$x = 1;
$path = $_SERVER['DOCUMENT_ROOT'].'/test.txt';
$file = fopen($path,"w");
while($x <= 5) {
echo fwrite($file,"new product".$x);
$x++;
sleep(5);
}
fclose($file);
}
Thing is that I do not completelly understand how CI loads view files. In given example I have to wait 25 second (5*5) until view is loaded. So the question have to parts:
How to force loading view immediately?
How to set ignore_user_aborts() in CI?

header redirect with include file

I've looked around on here and couldn't locate any solution for my little problem.
I currently have this following IF statment
if($page==1) {
$sql = "UPDATE page SET page='1' WHERE id=1";
INCLUDE 'month.php';
header("Refresh: 10; url=http://XXXXX.co.uk/?p=2");
}
Which currently updates a database according to what page it is on and then includes the current month calendar file and I hoped I could redirect it to page number 2 which is very much similar but loads another calendar and the SQL adds +2 to the database.
In essence it's just a loop I've made and it just reads a SQL database to see what page it needs to load next< I know there's better ways to create a loop (just to scroll through 3 PHP pages) but I'm not that great at PHP..
So i'm just wondering really, could anybody help me to have that include statement along with the header refresh?
Like said the purpose is so that the pages rolls a loop, I've managed to get it all to work with an iframe (instead of the header) but it takes a few seconds longer for the frame window to load.
#Mario that's perfect - I echoed the following:
if($page==1) {
$sql = "UPDATE page SET page='1' WHERE id=1";
echo '<meta http-equiv="refresh" content="5;url= http://XXXXX.co.uk/?p=2"/>';
INCLUDE 'month.php';
}
Which now loads the loop flawlessly - I've been up all night trying to get that working.
Thank you so much
Can I push page transitions I wonder?
Such as 'push out' or 'scroll out' ?

Drupal block code appears to run twice

I am creating a new debit for a customer using the following code.
The problem is that when I check the balanced marketplace the customer has been debited twice.
<?php
Balanced\Settings::$api_key = "*********************";
$customer = \Balanced\Customer::get("/v1/customers/CU34xY6f9bKZzb0kjBxWTUjC");
$debit = $customer->debit('7000');
foreach ($debit as $key=> $value) { echo "$key : $value <br>"; };
?>
The first debit created shows up as the variable $debit returned by balanced, then there is a second one created and shown in the marketplace. If the code was running twice I would have expected to see the second one.
I have attempted the solution which has been mentioned for drupal running code twice
<?php
if (!function_exists('f')) {
function f() {
/* Code goes here */
return $new_debit;
}
}
$debit = f();
?>
The code is running as php in a block and activated on page load and I think the problem is related to blocks.
We solved the problem by putting the code is directly into the page body. It runs once and it does not matter if the teaser is set to hidden or not.
We do not intend to use blocks and when we have finished our testing we may be moving into a custom module. I am posting this in case anyone else has a similar problem and this helps them and also because I want to know why this is happening with blocks.
This problem is related to putting PHP code directly into a block.

PHP: Dynamic Pages with multiple dynamic content

I'm using this a particular code to dynamically load content on a webpage.
Problem is, I want to use it to load multiple things on the same page. How do I go about making this possible? I've already isolated the problem to the fact that the second instance of the code that runs replaces the entire URL instead of appending an additional ?="pageurl" but I'm stuck right there.
I've got a second script that uses the variable p instead of b so the browser knows to load p's content in a different location.
This is what the code does:
http://www.youtube.com/watch?v=Qg-EBdNaUbo
[edit]
Not sure why you guys can't see the link, it explains everything. I'm calling the script via a link Here is the link again youtube.com/watch?v=Qg-EBdNaUbo
CODE:
<?php
$pages_dir = 'pages';
if(!empty($_GET['b']))
{
$pages = scandir($pages_dir, 0);
unset($pages [0], $pages [1]);
$b = $_GET['b'];
if(in_array($b, $pages))
{
include($pages_dir.'/'.$b);
}
else
{
echo 'sorry page not found';
}
}
else
{
}
?>

Access control of page in php

I want to control the access in php website.
I have a solution right now with switch case.
<?php
$obj = $_GET['obj'];
switch ($obj)
{
case a:
include ('a.php');
break;
default:
include ('f.php');
}
?>
But when i have so many pages, it becomes difficult to manage them. Do you have better solutions?
Right now, i develop the application using php4. And i want to use php5. Do you have any suggestions when i develop it using php5?
Thanks
$obj = $_GET['obj'];
$validArray = array('a','b','c','d','e');
if (in_array($obj,$validArray)) {
include ($obj.'.php');
} else {
include ('f.php');
}
The more pages you have the harder it will be to control this.
Your best off using a framework of some sort, my personal preference is CodeIgniter.
why not just address a page itself?
first page
another page
I am not saying that this is the best solution, but years ago I used to have a website which used a database to manage the key, the page to be included, and some informations like additional css for instance.
So the code was something like:
<?php
$page = htmlspecialchars($_GET['page']);
$stuffs = $db->query('select include,css from pages where pageid = "' . $page . '" LIMIT 1');
?>
So when we needed to add a page, we just created a new field in the database. That let us close a part of the website too: we could have a "available = {0,1}" field, and if zero, display a static page saying that this page was under maintenance.

Categories