Hello ,
I am stuck on this line of code

echo $hits[0];

i am trying to get it to echo into a text box as an integar. at the moment the code

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>

when run on

<?php
    include ("counter.php");
    ?>

just gives me a number
I have been thinking along trying to get

<?php
    include ("counter.php");
    ?><HTML><HEAD>
</HEAD><BODY><INPUT value="ehco $hits" name="Hit counter"></BODY></HTML>

to work ,
thanks.

Dani AI

Generated

Two quick points before code: keep the counter logic separate from the HTML, and never include a file that directly echoes when you expect its value inside an attribute. was right to try putting the counter into the input value, but if counter.php echoes you can break the HTML. ’s example also omitted quotes around value, which will break the attribute — always quote attributes and escape output.

Create a counter file that increments and returns the integer (no echo):

<?php
// counter.php — increment and return the new count (no direct output)
function increment_hit_counter($file = 'hitcounter.txt') {
    $path = __DIR__ . DIRECTORY_SEPARATOR . $file;
    if (!is_file($path)) {
        file_put_contents($path, '0', LOCK_EX);
    }
    $count = (int) @file_get_contents($path);
    $count++;
    file_put_contents($path, (string) $count, LOCK_EX);
    return $count;
}

Call that from your page and safely insert the number into the input:

<?php
require_once 'counter.php';
$hits = increment_hit_counter(); // integer, no extra output
?>
<input type="text" name="hit_counter" value="<?php echo htmlspecialchars($hits, ENT_QUOTES); ?>">

Troubleshooting tips: make sure hitcounter.txt is writable by the web server (proper owner or chmod), remove any BOM/extra whitespace in counter.php (it can pollute the HTML), and use file locking or a database for high-traffic sites (file-based counters can race). If the field is for display only, use readonly or type="number" to reflect intent.

first off lets fix the counter.php code

<?php
$count_my_page = ("hitcounter.txt");//please take in consideration that the file hitcounter.txt already exists
$hits = file($count_my_page);
$hits[0]++;
$fp = fopen($count_my_page , "w");
fputs($fp , $hits[0]);
fclose($fp);
echo $hits[0];
?>

And now to embed the code in a textbox

<html>
<head>
</head>
<body>
<input name="Hit Counter" type="text" value="<?php include ("counter.php"); ?>">
</body>
</html>

I hope this solves your problem
please reply back if things didn't work for you.

<?php
include ("counter.php");
?>
<HTML>
<title>
Verified by tomato.pgn
</title>
<HEAD>
</HEAD>
<BODY><INPUT type="text" value=<?php echo $hits ?> name="Hit counter">
</BODY>
</HTML>

Hope this will fix ur error.....
if any other problem PM me....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.