PHP - Server side functions and variables? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
(PHP)
So here is what I would like to achieve.. but apparently I can't find any relevant information anywhere.
I would like my site to select a random value from my database every 5 minutes (for example: an ID from a table), and everyone that visit my site would see that same selected value, until the "server" randomly select another value 5 minutes after the last select.
So, I guess there would be a function that do the select from the database,
but
1) where would I implement that function?
2) where would I call it? I'm not a expert in PHP, but I don't think it can be called from the client, else every client would call the function and they would not see the same value as others?
3) how do I to set a 5 minutes timer to call the function again?
In brief.
Server select a random value (ex: an ID from a table).
It get displayed on the site (same value from everyone for 5 minutes)
5 minutes after last select, another value is randomly selected
It get displayed..
Repeat...
I will probably use Laravel to created my website, I'm saying just in case it's important for the solution.
Thank you for your help!!

1) As far as implementing functions goes, It is pretty simple. If you need to use your function in multiple PHP documents, place int in a separate file and use an include to bring it into the document(that the client calls):
<?php include_once($_SERVER['DOCUMENT_ROOT']."/Path/To/file/phpscript.php");
If you only need it in one document, simple place it in in the document(that the client calls) before you use it.
2 & 3) There are "cron jobs" if you'r using Linux(I personally don't like using them), which could be used to awaken the php script every 5 minuets(in which case keep the function in its own file), but instead I would (edit: not, use cron jobs, this is a last resort)recommend another method instead. In your function(which will run when the client requests it), write the time you last requested it in to some text file, database or anything else. when the function runs, check if the written time was more than 5 minuets ago. If it was, create a new number and write a new time down. If it wasn't, fetch the old number from the database.
EDIT: Use Cron Jobs

Related

Getting PHP to run once on a specific date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've looked into this, and I've found out about Cron jobs - but the thing is that it only has to run once ever.
Background:
My users can create something that expires - and they choose when it expires.
One of my solutions: Maybe I don't have to expire it on that day - whenever someone requests that thing, then I can check whether or not it has already expired, and if it has, then I can change that thing accordingly.
Why it wouldn't work: I'd also like to send an email out when that thing expires.
Thanks for any help!
You could save a time-stamp together with the user item, that you call expire for example
then you could run a cron job that removes all items that has passed the expired time.
if($item.expire < time())
{
// Item has expired, remove it
}
You can set up a cron job that runs a script which compares the date with the expriation date. If It is equal to the expiration date, send a mail, else do nothing.
If you re on MYSQL you could use events. I ve used them successfully on expiring sessions.
You could choose to have kind of "runner" that checks for available jobs to run at the specific time and runs them.
Then you can create a cronjob that fires the runner.
Cron jobs are what you're looking for, you just have to find a way to run the task only once.
https://stackoverflow.com/a/5473841/2055152
Try this out to execute a command on 30th March 2011 at midnight:
0 0 30 3 ? 2011 /command
This answer offers a simple solution. If you want users to create their own events with PHP, just convert their desired date to the cron format. Just be careful on what you allow them to launch!

How to make the server do things after some database actions? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The title might be a bit unclear. But I have this problem with my website. I have some data stored in a sql database, including a timestamp. But now I need to make some calculations with that data and send an email, after a certain duration after that timestamp. But how do I do this? Since my website and my database only contain static data...
So to be clear: I have a site on shared hosting with a database. In that database are items that all contain a timestamp value. Now I need the server to undertake an action for every item in the database on a certain duration after that item's timestamp, for example send a mail.
This is probably an easy question, but I have no clue what to look for...
EDIT: I think I have found a solution by using setcronjob.com , thanks!
You could run a job every so often to figure out how old that timestamp is and then send the emails you need to send. But how to run the job?
If you can run scheduled jobs on your hosting (ie, you own the machine/VM and are not on shared hosting) you can run these jobs every x days/hours/minutes/seconds. In Linux, look up cron jobs, and Windows look up Task Scheduler (I think)
If you're on shared hosting it's more difficult. Maybe you can convert some of your static pages into dynamic pages that run these jobs? Depending on how long they take to run you might want to look at running these jobs in a new thread (pthreads). You'll need to have pthreads installed for this on your hosting, so #1 - scheduled jobs - is definitely more preferable.
You probably want to use a Cron task to search your database for every row with the timestamp near the current time. From that point, you can simply do your calculations and send the email.
Could you tell me more about that "certain duration after that timestamp"?
Also, in the database you could setup a job that runs on a schedule to make the calculations and email the results.

How often can I query MySQL database through PHP [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a microcontroller which reads temperature from a sensor every second and serially sends it to a computer attached to it via USB. The computer has a LAMP server running on it. It takes the temperature measurements from the microcontroller and adds them to a MySQL server running on the computer. There is also a PHP file on the computer which is able to read the most recent temperature from the database and "echo" it. I have an Android app which queries the PHP file to get the temperature and update it on the screen.
Now, the question is: All of this process is happening on a local area network, so it's pretty fast. Would it be a good idea to "poll" the server every second if this was happening over the internet? How often should I ideally query the PHP file if I was doing this over the internet?
It just depends.
You can query your database as often as you can, however you may meet efficiency issue.You can just write a script which just query your database and set a clock to make that script just run for sometime (30 seconds for example). You count the total query, and output the count into file.
Use this method you can test the max qps of your database with that specific query.
Normally, you should reduce the frequency of every database query, cause the database query may be the bottleneck of your script speed.
You can implement some asynchronous method to update it. Although there is no limitation on how many times you can query but it can hamper your server performance if too many connections are open. So you have to think of some logic or time which will suit your need (get updated data to user) and also not hamper server performance. You can also lower the burden by just querying updated data not entire data (as mentioned by #Rikesh)
Create a updateTime field in your table. Each time you update the row update this field with current time. When you are querying use this field. For the first time the updateTime = 0. store the max updateTime values and use it for the next call. So that each time you are fetching rows that are updated after the given time.
Another technique was cache the fetched values and make a query to fetch the data that are updated after the cache created. And sync the cached data with the updates.
If you query and fetch all data often( 1 sec as you mentioned in question), it will affect the server performance.
Use a cron job e.g set the script to run every so and so minutes...or you could use ajax to call your script every second or on events such as button clicks. If what you are aiming for is a real-time app you might want to look into some real-time technologies such as comet long-polling(facebook notifications), websockets, xmpp or as I said ajax!
If you want to show/get the only update values and reduce the time then you have to compare/check the id value with the previous one,it means
save the current retrieved id or any unique value from row(like datetime)
and for the next time check the id or datetime values (like select * from table where datetime>currendatetime)so that you can get only update rows

How to create temporary record in MYSQL which will get deleted after 30 minutes or closing of browser? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am building a web application that would requires me to reserve a seat for the customer for 30 minutes or closing of the browser before the reservation gets deleted. Any suggestion of the best way to do this?
I thought of adding a record into MYSQL and runs a cron job but that would not give the users the most updated results. Any advises?
Thanks!
Write a record with an expiration date. Use that expiration date when writing queries. Clean up expired records every so often.
Closing the browser however is more complicated and not reliable. Connections to the server in http are stateless and as such you really wont know when the browser has been closed. You can regularly poll with ajax and delete records when there are no longer any poll updates.
This problem is actually 2 problems:
How do you clean up old records
and
How do you ignore expired records.
Add an expire_time field to your reservations table. You probably have some logic that looks at how many seats are still available, add the expire_time to that logic so that expired records are ignored.
Then write a script or function that gets called periodically (cron works, so does register_shutdown_handler()) to clean up the expired records.
This way, should your cron ever fail or be slow for some reason, you're not accidentally blocking new reservations. You could even skip the cleanup step completely and take the (minor) performance hit.
I believe it should be resolved by session, not the mysql.
But if you insist, you should update item every time page get hits, and you can do also some sweepups
EDIT
Yes, of course. The reservation system must be multi user. If one user does the reservation than the other user must not be able to get the seat.
Yes, simple resolution is just to add there new column called reserved that will specify the time until it is reserved.
Why do you need cron for it?
BTW: sessions are stored on server not client. cookies are stored in client not server.

Running a scheduled task in a Wordpress plug-in [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to write a Wordpress plug-in that automatically posts a blog post at a certain time of day. For example, read a bunch of RSS feeds and post a daily digest of all new entries.
There are plug-ins that do something similar to what I want, but many of them rely on a cron job for the automated scheduling. I'll do that if I have to, but I was hoping there was a better way. Getting a typical Wordpress user to add a cron job isn't exactly friendly.
Is there a good way to schedule a task that runs from a Wordpress plug-in? It doesn't have to run at exactly the right time.
http://codex.wordpress.org/Function_Reference/wp_schedule_event
pseudo-cron is good but the two issues it has is
1, It requires someone to "hit" the blog to execute. Low volume sites will potentially have wide ranging execution times so don't be two specific about the time.
2, The processing happens before the page loads. So if teh execution time happens and you have lots of "cron" entries you potentially upset visitors by giving them a sub standard experience.
Just my 2 cents :-)
vBulletin uses a sort of Pseudo-Cron system, that basically checks a schedule on every page access, and fires any processes that are due then.
It's been a while since I worked with Wordpress, but I think something like this should work if it runs on each page view.
I think the best way to do this is with a pseudo-cron. I have seen it on several occasions, and although not exact in the timing, it should do what you need it to do. Since in Wordpress the index.php is the first thing always hit based upon the settings in the .htaccess, create a file called pseudo-cron.php, dump it into the root directory, and then require it once from the index. Whenever someone hits the site, it will run, and you can use it to initiate a script, and check if another daily digest needs to be generated depending upon the time of the day, and when the previous digest ran.

Categories