$file = ".$dirN/$filename.";
$count_my_page = (".$dirname1/$filename.$extens");
if(fopen(".$file.","r"))
{
$hits = file($count_my_page);
$nHits = ((int) $hits[0]) + 1;
$fp = fopen($count_my_page , "w");
fputs($fp , $nHits . "");
fclose($fp);
echo $nHits;
}
I want the count to increase only when i read the file, but it happens other wise on refreshing the page where the link of the file is present.
Clicking a link and refreshing a page are only differt actions on the client, there's no difference to the server; it just sees a HTTP request.
I don't think this can be done without using JavaScript to set some kind of form variable when the link is clicked.
iam not sure if i understand the question but you can try the following :
Add an parameter to the link used to read the file like :
http://www.localhost.de/readfile.php?read=true
in your count method check for the parameter:
if ($_request['read'] === true) {
// count
-> do an redirect to remove the parameter, or set it to false ..
} else {
// nothing
}
Related
Okay, my title is probably not describing especially much, however, here's my PHP code:
<h1>404 Server Error</h1>
<?php
$ip = $_GET['ip'];
if($ip) {
$ip = str_replace('%3A', ':', $ip);
$ip = "http://" . $ip;
for($i = 0; $i < 11; $i++) {
$ch = curl_init($ip); // (Also tried file_get_contents($ip); instead, but didn't work
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
curl_exec($ch);
curl_close($ch);
}
}
?>
I basically want it to load a page that will look something like:
http://123.456.789:80/
(which if you go to the actual site, it will return that the server/site was not found)
But it still gets through the message (I've tried so I know this)
Anyway, CURL nor file_get_contents will work for this as they expect something in return and just keeps running for ages, as they try find the page. But I want them to send a request to the page, then cancel, how would I do this?
TLDR: I want to make the php script send a request/load a page for just a millisecond/second even if it can't load the actual page.
Thanks in advance.
TEMPSOLUTION: I'm using <iframe src='" . $ip . "'></iframe> as a temp-solution.
to load a website with php use:
header('Location: www.yourwebsite.com');
I've placed a hit counter on my page. It reads a text file, increments the number in the file, and later in the page, I output the incremented value.
$hitsFile = "hits/exps/stats.txt";
$hits = file($hitsFile);
$hits[0]++;
$fp = fopen($hitsFile , "w");
flock($fh, LOCK_EX);
fwrite($fp , $hits[0]);
fclose($fp);
My problem is that if I reload the page, the code will increment the hits. I don't want that. I thought of using session to fix that, but with session, in order the increment the hits again, I need to exit the site and visit again. I don't want that either.
I want it to increment not when I reload the page but when I revisit the page.
For example, let's say I have two-page website, Home and Contact, and on contact page I have a hit counter. I don't want the hit counter to increment if I reload(refresh) the contact page, but if I leave the contact page and visit homepage, and later revisit the contact page, I want it to increment.
In short, I don't want it to increment on page reload. Is there a way to do that?
In each of your pages, you need to write the page name in the session.
Do something like this:
$_SESSION['page'] = 'contact';
On the pages where you need to count hits, you need to check this session key.
For example, if you were on page 'contact', then $_SESSION['page'] == 'contact'.
Now when you go to visit the 'homepage':
$page = $_SESSION['page'];
if($page != 'homepage')
{
//increment your hits counter
$_SESSION['page'] = 'homepage';
}
I suggest this method, is my preferred, create in root these folders: cnt and log... then put inside cnt folder the following files cnt.php and showcnt.php...
cnt.php
<?php
##############################################################################
# Php Counter With Advanced Technology For The Prevention Of Reloading Pages #
# Version: 1.4 - Date: 13.11.2014 - Created By Alessandro Marinuzzi [Alecos] #
##############################################################################
function cnt($file) {
session_start();
global $pagecnt;
$reloaded = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
$thispage = basename($_SERVER['SCRIPT_FILENAME']);
if (!isset($_SESSION['first_go'])) {
$_SESSION['first_go'] = 1;
$first_go = TRUE;
} else {
$first_go = FALSE;
}
if (!isset($_SESSION['thispage'])) {
$_SESSION['thispage'] = $thispage;
}
if ($_SESSION['thispage'] != $thispage) {
$_SESSION['thispage'] = $thispage;
$new_page = TRUE;
} else {
$new_page = FALSE;
}
$pagecnt = rtrim(file_get_contents($file));
if ((!$reloaded) && ($new_page == TRUE) || ($first_go == TRUE)) {
$fd = fopen($file, 'w+');
flock($fd, LOCK_EX);
fwrite($fd, ++$pagecnt);
flock($fd, LOCK_UN);
fclose($fd);
}
}
?>
showcnt.php
<?php
##############################################################################
# Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
##############################################################################
function gfxcnt($file) {
global $number;
$number = rtrim(file_get_contents($file));
$lenght = strlen($number);
$gfxcnt = "";
for ($i = 0; $i < $lenght; $i++) {
$gfxcnt .= $number[$i];
}
$gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
echo $gfxind;
}
?>
Well, then edit your index.php or other php page... and put at the beginning this piece of code:
<?php session_start(); include("cnt/cnt.php"); cnt("log/index.txt"); include("cnt/showcnt.php"); ?>
Well, then edit index.php or other php page... and use this piece of code for reading counter file:
<?php gfxcnt("log/index.txt"); ?>
It's all, I hope you'll find my answer useful :) My counter can write/read multiple php pages...
Source: my blog (https://www.alecos.it/new/101/101.php)
Add session_start(); to the top.
Now change your if to this:
if (!isset($_SESSION['lastpage']) || $_SESSION['lastpage'] != $_SERVER['QUERY_STRING') {
$hits[0]++;
}
$_SESSION['lastpage'] = $_SERVER['QUERY_STRING'];
This will basically force someone to move to another page if they want to increment the counter.
Update the hit count only if the current URL is not stored in $_SESSION['url'].
After updating the hit count, store the current URL in $_SESSION['url'].
// Read Get cerruncy from URL request
if(in_array("crncy",$get_page_all)) {
$crnc_set = explode("/crncy/",$open_page);
$crnc = $crnc_set[1];
$get_crnc = (string) $crnc;
$_SESSION['xxxc'] = $get_crnc;
}
if($_SESSION['xxxc']){
echo $_SESSION['xxxc'];
}
else {echo 'Noooooooooo';}
My problem: In next refresh $_SESSION['xxxc'] value gets changed.
Use session_start(); at top of the page.
I´m testing a php exercise, and I can´t get it working properly. It´s a counter that stores the visits inside a txt file as a simple integer. Every time I reload the page the number gets a +1.
Now, I want it to reload only when there´s a new visit, so I´m trying sesion_start() for the first time.
In class, this example worked just fine, but when I try to reproduce it at home, the number won´t change, even if I close the browser and open it again.
This is my code (it´s inside the php tags, naturally):
session_start();
if (!$_SESSION[contador]) {
define('ARCHIVO', 'visitas.txt');
if (file_exists(ARCHIVO)) {
$fp=fopen(ARCHIVO, 'r');
$cant=fread($fp,filesize(ARCHIVO));
fclose($fp);
} else {
$cant=0;
}
$cant++;
$fp=fopen(ARCHIVO, 'w');
fwrite($fp, $cant);
fclose($fp);
$_SESSION[contador]=$cant;
}
echo '<h3>Hay '.$_SESSION[contador].' visitas.</h3>';
contador should be in quotes unless it's a defined constant somewhere?? I assume it's a string
$_SESSION["contador"]
Try this out. It's different, but it'll do something at least. Would've posted as a comment, but I wanted code formatting.
session_start();
if(empty($_SESSION['contador'])){
$_SESSION['contador']=1;
}else{
$_SESSION['contador']++;
}
echo '<h3>Hay '.$_SESSION['contador'].' visitas.</h3>';
First of all, #Lenny is correct. $_SESSION['contador'] is what is called a session variable or a variable assigned to the $_SESSION array. So it MUST BE in quotes. That is not optional. And you cannot define a constant and use that value inside the brackets.
if you had a constant, you could set the session variable to that. But, since your value is variable, as you are adding incrementally, it is by definition not constant.
Furthermore, refreshing your Web browser will keep your session active. So if you truly want to test this, try session_destroy(); at some point.
Here is how this code will work (the storage of the session variable anyway):
<?php
session_start();
if(!isset($_SESSION['contador'])) {
define('ARCHIVO', 'visitas.txt');
if(file_exists(ARCHIVO)) {
$fp = fopen(ARCHIVO, 'r');
$cant = fread($fp,filesize(ARCHIVO));
fclose($fp);
} else {
$cant=0;
}
$cant++;
$fp = fopen(ARCHIVO, 'w');
fwrite($fp, $cant);
fclose($fp);
$_SESSION['contador'] = $cant;
}
echo '<h3>Hay ' . $_SESSION['contador'] . ' visitas.</h3>';
?>
Note: your logic is bad. Your are telling your script that $cant is either equal to the value of the txt file OR it is equal to 0. Then your are incrementing that value by one. You will perform that task on each load. You need to modify this code and finish your conditional statement.
<?php
session_start();
if(!isset($_SESSION['contador'])) {
define('ARCHIVO', 'visitas.txt');
if(file_exists(ARCHIVO)) {
$fp = fopen(ARCHIVO, 'r');
$cant = fread($fp,filesize(ARCHIVO));
fclose($fp);
} else {
$cant=0;
}
$cant++;
$fp = fopen(ARCHIVO, 'w');
fwrite($fp, $cant);
fclose($fp);
$_SESSION['contador'] = $cant;
} else {
$_SESSION['contador']++;
}
echo '<h3>Hay ' . $_SESSION['contador'] . ' visitas.</h3>';
?>
I hope this helps.
I am trying to create a dynamic website, the index.php includes the following code in the content area of the website:
<?PHP
// if undefined then define
if(!$od || $od == ""){
$od = "news";
}
// check if exists prep
$link = 'incs'."/".$od.$ext;
flush();
$fp = fopen($link, "r");
// check if inclusion file not exists then return error
if (!$fp) {
echo "An error has ocurred while processing your request. The file linked as ?od=".$od." does not appear to exist.";
}
// if exists then return parse
else {
fclose($fp);
include 'incs'."/".$od.$ext;
}
echo '</body>'."\n";
echo '</html>'."\n";
?>
I also have various links throughout the site to pages like register, login, etc.
Those links point to pages like ?od=register, ?od=login, etc.
The website will pull the default file for me, news, and display that in my content section of my website, but when I click register, the url in the address bar DOES change to /?od=register, but the default news remains in the content section, is there an error in the code above? Or am I just missing something?
P.S. $ext is defined in my config file as inc.php, which is included at the top of the index page.
Besides the fact that this is very insecure - you are making a GET request, access the variables through the $_GET array ie $od = $_GET['od']
I believe you need to define $od with a $_GET['od'] or $_REQUEST['od']
$od = $_GET['od'];
// if undefined then define
if(!$od || $od == ""){
$od = "news";