Good day!
I am getting all the applications that you can see in the task bar (with the help of powershell) and place all the results in the notepad. This script would run for every minute. I am trying to make a php code on which it would fetch the contents of the notepad and try to get rid of the redundant apps that has been placed on the notepad and would only get the newly opened ones then print it out. For example, I have opened
first minute
notepad
chrome
next minute
notepad
chrome
firefox -> this would be the only one to be printed out
I only want the newly opened one to be printed out.
What I am trying right now is to run 2 powershell script (one in every minute and one in every 2 minute) so I can have two outputs, get the difference from the array and print it out but it seems like its not a proper way to do it.
Codes I have right now:
<?php
$filename1 = 'C:\Users\Fluke\Desktop\apps\runningapps1.txt';
$filename2 = 'C:\Users\Fluke\Desktop\apps\runningapps2.txt';
if ($file1 = fopen($filename1, "r")) {
$read1 = fread($file1,filesize($filename1));
$line1 = explode("\n", $read1);
$file2 = fopen($filename2, "r");
$read2 = fread($file2,filesize($filename2));
$line2 = explode("\n", $read2);
$result1=array_diff($line1,$line2);
$result2=array_diff($line2,$line1);
echo "1";
print_r($result1);
$output1 = print_r($result1, true);
echo "<br>";echo "2";
$output2 = print_r($result2, true);
print_r($result2);
var_dump($line1);
var_dump($line2);
}else
echo "No newly opened apps";
}else
die("Unable to open file!");
?>
Is there any other way than what I am doing right now? I'm really new to this. Hope you'll understand.
Related
I have this small piece of code on my website that i use to count downloads.
its pretty simple really,
counter.php sends the command and counter.txt is just a 1 line text file with a number that auto goes up every time the link is clicked.
My question is, is it possible to have 2 counter.txt files and add them to a third counter.txt file? so it would look something like:
counter.txt + counter2.txt = counter3.txt ?
$counter = 'counter.txt';
$download = 'downloadurlhere';
$number = file_get_contents($counter); // read count file
$number++; // increment count by 1
$fh = fopen($counter, 'w'); // open count file for writing
fwrite($fh, $number); // write new count to count file
fclose($fh); // close count file
header("Location: $download"); // get download
So essentially i want to offer 2 downloads
a light version and a full version
and then keep track of each download count separately. but then also have a count for the total of both downloads.
oh and to make sure i include enough detail, on the download.php page i echo the counter.txt file with
<?php echo file_get_contents('counter.txt');?>
Adding the counters of two files should be easy. You just have to read each file and then the variables are added and written to third counter file or whatever you desire. An example:
<?php
$first=file_get_contents("counter1.txt");
$second=file_get_contents("counter2.txt");
$sum=$first+$second;
file_put_contents("counter3.txt",$sum);
?>
So here was the end result, I had to swap around the txt files for counter.txt and urls but this worked perfectly...
$count = 'counterfull.txt';
$total = 'total.txt';
$download = 'URL';
$number1 = file_get_contents($count);
$number1++;
$fh = fopen($count, w);
fwrite($fh, $number1);
fclose($fh);
$number2 = file_get_contents($total);
$number2++;
$fh = fopen($total, w);
fwrite($fh, $number2);
fclose($fh);
header("Location: $download");
Hellooo,
I wrote myself a little PHP experiment. This script counts how many times the user clicked a button labeled with a specific class (id="link_1", class="heart")
During each click, the script reads a txt file, finds the right id, then adds +1 to that id's number, like so:
#counte_me.php
$file = 'count_me.txt'; // stores the numbers for each id
$fh = fopen($file, 'r+');
$id = $_REQUEST['id']; // posted from page
$lines = '';
while(!feof($fh)){
$line = explode('||', fgets($fh));
$item = trim($line[0]);
$num = trim($line[1]);
if(!empty($item)){
if($item == $id){
$num++; // increment count by 1
echo $num;
}
$lines .= "$item||$num\r\n";
}
}
fclose($fh);
file_put_contents($file, $lines, LOCK_EX);
The result
# count_me.txt
hello_darling||12
This works wonderfully well. The problem happens when, from time to time, I find myself staring at an empty count_me.txt!
Not really know when or how it happens, but it does. I start making increments and happens, sometimes sooner, sometimes way later. It may be on my way to 10 or to 200 or to 320 or anything in between. Its totally random.
Driving me crazy. I'm not experienced enough, but that's why I'm playing with this thing.
Someone knows what I am doing wrong here for the file to get dumped like that?
UPDATE 1
So far, Oluwafemi Sule's suggestion is working, but I have to remove LOCK_EX from the file_put_contents for it to work, otherwise it just doesn't.
// NEW LINE ADDED
if (!empty($lines)) {
file_put_contents($file, $lines);
}
$lines is initially set to an empty string and only updated on the following condition.
if(!empty($item)) {
# and so on and so on
}
And finally at the end,
file_put_contents($file, $lines, LOCK_EX);
The reason that $lines still remains set to the initial empty string happens when item is empty. Remember the newline added from "$item||$num\r\n", there could be more than a single line added there(I won't put it past a text editor to add a new line to end that file .)
I suggest to only write to the file when $lines isn't empty.
if (!empty($lines)) {
file_put_contents($file, $lines, LOCK_EX);
}
I'm Trying to get the links of all script tags of any given site (will only be trying it on my own personal sites). Using simplehtmldom http://simplehtmldom.sourceforge.net/manual.htm I have a working bit of code but no idea how or if it is even possible to run this script on a lamp server in the background once an hour, forever. By in the background I mean without a user actually on the site. Is there a simple way to achieve this? even a quick botched (simple) way would be great! Thanks.
require 'simple_html_dom.php';
function logToFile($filename, $msg)
{
// open file
$fd = fopen($filename, "a");
// write string
fwrite($fd, $msg . "\n");
// close file
fclose($fd);
}
$html = file_get_html('randomsite.com');
// set default timezone
date_default_timezone_set('Pacific/Auckland');
$current_date = date('d/m/Y | H:i:s');
// set scripts src
$current_src = '';
$scripts = $html->find('script');
foreach($scripts as $s) {
if(strpos($s->src, 'jquery') !== false) {
//do nothing
}else{
$current_src = $current_src . $s->src . ' ';
}
}
echo $current_src;
logToFile("data.log", "$current_date : $current_src".PHP_EOL);
Yeah,this is possible. If you want to run any script every hour than you can use cronjob in which you have to define like below cronjob and it will work fine.
Syntax :
minute hour day month day-of-week command-line-to-execute
if you want to every hour than need to select like below
*1 * * * php /var/www/html/VMonitor/download_file.php
I have file1.txt and A PHP Code which increment it.
I have a snippet to display the image.
I paste a snippet in my website that displays Images by reading the data number from file1.txt.
I use the snippet multiple times in one page but I get the same result. What happens is that when the website page is loaded it reads the file1.txt having ,lets say number 10, image number.
What I want is that Snippet 1 Displays 10 Increment file1.txt
then Snippet 2 Displays 11 and so on.
how should i make the snippet in such a way that it waits for the increment and then reads it and then it displays image.
Example:
Snippet 1 >10th Image
Snippet 2 >11th Image
Snippet 3 >12th Image
While all are executed in one page?
One Way i Think might be that the snippet GET the wait.txt and
checks if its 1 or 0 If It is 1 it waits for certain seconds then
execute the external php and if it is 0 it proceeds to executes the
external PHP. Then External PHP on Execution Sets wait.txt to
1 while its being used and then once it has finished it sets the
wait.txt to 0
But I doubt it will flawlessly work. I need you guys to help me out.
Okay the below code is what I have
The PHP Contains this:
$open = fopen("xml/file1.txt", "r+");
$num = fgets($open);
$close = fclose($open);
$num++;
$open = fopen("xml/file1.txt", "w+");
fwrite($open, $num);
$close = fclose($open);
The Snippet
<?php
$myFile = "../xml/file1.txt";
$line= file($myFile);//file in to an array
$image = $line[0]; //So that it Reads the first line if 2nd line has /n
echo "<img src=''.$image.'.img'></img>";
/*Executing PHP through php as it as additional code in php
which does some other verifications.*/
echo '<script src="//localhost/image.php"></script>';
?>
The file1.txt Contains:
1
//Ignore the comment here it just contain 1 number
//which increment each time the snippet executes the php
So I paste The snippet in one page Multiple times. But All snippet displays the same image instead of in ascending order. I want to find how can I do it? No MySql Database. And I don't want to list and read each line.
I want my snippet to read the data from file1.txt after it has been
updated
I want my snippet to know that the php is busy and needs to wait while other snippet is using it. As my project will include heavy traffic I want it Optimized.
Lastly I am a new to this so it's hard to figure out.
THE CODE BELOW WORKS BUT HOW CAN I OPTIMISE IT for heavy traffic?
<?php
session_start();
waitque();
function waitque() {
$input = $_SERVER['HTTP_HOST'];
$e = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$input = trim($input, '/');
// If not have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
$urlParts = parse_url($input);
// Remove www.
$dom = preg_replace('/^www\./', '', $urlParts['host']);
if(isset($_SESSION['worker'])){
$open = fopen("queue/imageindex.txt", "r+");
$value = fgets($open);
$close = fclose($open);
$value++;
$open = fopen("queue/imageindex.txt", "w+");
fwrite($open, $value); // variable is not restated, bug fixed.
$close = fclose($open);
$myFile = "queue/imageindex.txt";
$line= file($myFile);//file in to an array
$relid = $line[0];
echo $relid." Code PUBLISHED</div>";
unset($_SESSION['worker']);
} else {
echo "<div>waiting...</div>";
$_SESSION['worker']= $dom.$a.$ip;
waitque();
}
}
?>
I have written a php script which parses this text file
http://www.powerball.com/powerball/winnums-text.txt
Everything is good, but I wish to control the amount that is download i.e. I do not need every single result maybe max the first 5. At the moment I am downloading the entire file (which is a waste of memory / bandwidth).
I saw the fopen has a parameter which supposed to limit it but whatever value I placed in has no effect on the amount of text that is downloaded.
Can this be done? Thank you for reading.
Here is a small snippet of the code in question which is downloading the file.
<?php
$file = fopen("http://www.powerball.com/powerball/winnums-text.txt","rb");
$rows = array();
while(!feof($file))
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
}
fclose($file);
?>
Thanks everyone this is the code which just downloads the first row of results
while(!feof($file))
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
if(count($rows)>1)
{
break;
}
}
fclose($file);
You can break whenever you don't need more data. In this example when count($rows)>100
while(!feof($file)) {
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
if (count($rows)>100)
break;
}
The issue is that your while condition is only met once you've read through to the end of the file. If you only want to get the first N lines you'll need to change that condition. Something like this might help get you started:
$lineCountLimit = 5;
$currentLineCount = 0;
while($currentLineCount < $lineCountLimit)
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
$currentLineCount++;
}
Please try the following recipe to download only a part of the file, like 10 KBytes first, then split to lines and analyze them. How to partially download a remote file with cURL?