Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Newbie needs recommendation for fast learning a little php.

0 views
Skip to first unread message

J.O. Aho

unread,
Jun 29, 2021, 3:02:00 PM6/29/21
to

On 29/06/2021 16.30, Not_even...@example.net wrote:
> I mostly need to do a specific thing, get a file to
> display its last modified date and time inside itself.
>
> I used to use ssi for this, but I moved to a new Web host and they
> seem to have their server time zone set as the only time zone I can
> display, and they are far away from me, so in ssi I get a date and
> time for a file modification time that's many hours off.

I'm assuming you got the PHP code to work to show the date (even if it's
the wrong timezone for you).

If you have stored the date in a variable $lastmodified then you could
use the following code to adjust it:

$date = date_create($lastmodified, timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP');

Just use a valid timezone name to replace the Pacific/Nauru. Keep in
mind that the time may look off for other users who do not live in the
timezone you want to display.

For more information about datetime and timezone check the following
page at php.net:

https://www.php.net/manual/en/datetime.settimezone.php


--

//Aho

Arno Welzel

unread,
Jun 30, 2021, 9:09:58 AM6/30/21
to
Not_even...@example.net:

> This does what I want, but only as a .PHP file. How do I get it into a
> file named foo.html so I can have foo.html declare when it was last
> modified?
>
> <?php
> $file = $_SERVER["SCRIPT_NAME"];
> $break = Explode('/', $file);
> $pfile = $break[count($break) - 1];

Why this?

> //echo $pfile;
> echo "This file was last modified on: " .date("Y-m-d
> H:m",filemtime($pfile));
> ?>
>
> I feel as if I'm almost there.

Much simpler:

<?php
echo "This file was last modified on: ";
echo date("Y-m-d H:m",filemtime($_SERVER["SCRIPT_FILENAME"]));
?>

Also see:

<https://www.php.net/manual/en/reserved.variables.server.php>

"'SCRIPT_FILENAME'

The absolute pathname of the currently executing script."


--
Arno Welzel
https://arnowelzel.de

J.O. Aho

unread,
Jun 30, 2021, 10:24:40 AM6/30/21
to
On 30/06/2021 14.08, Not_even...@example.net wrote:
> This does what I want, but only as a .PHP file. How do I get it into a
> file named foo.html so I can have foo.html declare when it was last
> modified?
>
> <?php
> $file = $_SERVER["SCRIPT_NAME"];
> $break = Explode('/', $file);
> $pfile = $break[count($break) - 1];
> //echo $pfile;
> echo "This file was last modified on: " .date("Y-m-d
> H:m",filemtime($pfile));
> ?>
>
> I feel as if I'm almost there.

I won't comment on the code as Arno already done that, there is two
options, I would say one option is not up to you, so in reality you have
only one option (the last one)

1. Make the webserver to send .html files to tne php engine as it does
with the .php files.

2. Rename the .html file to .php


--

//Aho


J.O. Aho

unread,
Jun 30, 2021, 4:23:34 PM6/30/21
to

On 30/06/2021 19.49, Not_even...@example.net wrote:
> Uh oh. But I'm reading that I can embed php into a html file. Isn't
> that possible?

The issue is that the web server will not treat each file it handles as
a php code, it will only look for files that ends with php, send those
to a PHP-engine which parses the file and executes the php code and then
hands back the output to the web server which then sends the output to
the end user (the visitor to your web page).

It's possible to allow .html/.htm files the same way, but then you need
the administration rights of the server, as you are using a shared
service, it means you don't have administrator privileges and those you
can't do the change.

So this leads to the only thing you can do, rename your files so that
they are called .php instead of .html/.htm.

If you don't want to rename files, then you need to look at javascript
solution for displaying last modified document time.

--

//Aho

Ray_Net

unread,
Jun 30, 2021, 5:03:24 PM6/30/21
to
In article <rdbmdghp8532b2sgk...@4ax.com>, Not_even...@example.net says...
>
> I don't even know if I'm up to the level of being a newbie at this
> point. I mostly need to do a specific thing, get a html file to
> display its last modified date and time inside itself.
>
> I'm not sure a book is going to be that useful, but maybe a Web site
> could be. I'd need one that really is basic.
>
> I've found a Web page or two, but they seem to assume that I know more
> than I do because I can't get the code snippets I've found on those
> Web pages to work.
>
> I used to use ssi for this, but I moved to a new Web host and they
> seem to have their server time zone set as the only time zone I can
> display, and they are far away from me, so in ssi I get a date and
> time for a file modification time that's many hours off. I'm hoping
> that php will solve my problem. Thanks in advance.

https://code.adonline.id.au/last-modified-date-php/

J.O. Aho

unread,
Jul 1, 2021, 2:03:26 AM7/1/21
to
On 01/07/2021 06.49, Not_even...@example.net wrote:
> On Wed, 30 Jun 2021 22:23:32 +0200, "J.O. Aho" <us...@example.net>
> Thanks for clarifying that. I don't control the server, but I've read
> that those who do can allow html files to be scanned for php code.
> Maybe my Web host has in fact done that. Would I be able to find some
> notation about that in the Web host's phpinfo.php file?
>
> Also, some scripts seem to work in html files and some don't.
>
> The following is the entire code for a file I named hello_1.html
>
> <html>
> <head>
> <title>PHP Test</title>
> </head>
> <body>
> <?php echo '<p>Hello World</p>'; ?>
> </body>
> </html>
>
> It produced the result:
>
> "Hello World
>
> '; ?> "
>
> in the browser. It's obviously got a little problem in the output, but
> it did produce a mostly useful output.

No, it's your web browsers html engine that detects a faulty html tag
with another html tag and tries it best to correct the html and shows
you what it thinks should be shown.

Had you written:

<?php echo 1+1; ?>

your output had been:


yes, nothing at all, and that is the proof of that the php code do not
be executed, had the php engine processed the page before sent to you,
you would have had a 2.


> Attempts to get the php script I actually want to use to run in an
> html file have not, so far, produced the thing the php script tries to
> do or else the file doesn't produce any result at all.

As long it's in a file ending with html/htm it will not be executed, so
matter what you write, nothing will happen, you need to change the
ending to php.

>> So this leads to the only thing you can do, rename your files so that
>> they are called .php instead of .html/.htm.
>
> Yeah, that would be a hassle, but if I can't get the php scripts to
> run in html files that may be what I'll have to do. I'd need to see
> how those php files would interact with the many html files they link
> to.

You just need to change the links to the new page name that ends with
.php and that's it.

>> If you don't want to rename files, then you need to look at javascript
>> solution for displaying last modified document time.
>
> I whipped up a javascript to do what I want days ago, but a lot of
> people keep javascript turned off, so it wouldn't work for them. I
> supposed I could figure out how to get some message put in place of
> the file modification date if javascript is turned off.

Itnernet is nowadays quite useless without javascript, so people tend to
not disable it anymore. The benefit with the javascript is that you can
display time the file was modified based on the users timezone.

--

//Aho

Arno Welzel

unread,
Jul 1, 2021, 4:49:20 AM7/1/21
to
Not_even...@example.net:

> On Wed, 30 Jun 2021 15:09:57 +0200, Arno Welzel <use...@arnowelzel.de>
> wrote:
[...]
>> Much simpler:
>>
>> <?php
>> echo "This file was last modified on: ";
>> echo date("Y-m-d H:m",filemtime($_SERVER["SCRIPT_FILENAME"]));
>> ?>
>
> Thanks for that. I've plugged this in and saved it as foo.php and I
> get an odd reaction. The actual time stamp on the file is 13:22, but
> the above script is telling me that the file was last modified at
> 13:06. I suppose that the idiots at my Web host may have screwed up
> something on the server. The 13:22 time is when I created the file, so
> it can't be that it's showing when it was created and not when it was
> modified. I'm scratching my head here.

You can also try this to make sure the file system cache is emptied:

<?php
clearstatcache($_SERVER["SCRIPT_FILENAME"]);
echo "This file was last modified on: ";
echo date("Y-m-d H:m",filemtime($_SERVER["SCRIPT_FILENAME"]));
?>

And if this doesn't work, use a global cache clear:

<?php
clearstatcache();
echo "This file was last modified on: ";
echo date("Y-m-d H:m",filemtime($_SERVER["SCRIPT_FILENAME"]));
?>

Also see: <https://www.php.net/manual/en/function.clearstatcache.php>

>> Also see:
>>
>> <https://www.php.net/manual/en/reserved.variables.server.php>
>>
>> "'SCRIPT_FILENAME'
>>
>> The absolute pathname of the currently executing script."
>
> Okay, I don't understand what you mean in the piece immediately above
> this. The above url seems to say that the script will just run.

$_SERVER is a super global array which provides a number of runtime
parameters of the current script. $_SERVER["SCRIPT_FILENAME"] contains
the complete name of the current script including the path or the name
which was passed at command line when using PHP as CLI.

Arno Welzel

unread,
Jul 1, 2021, 6:29:19 AM7/1/21
to
Not_even...@example.net:

> On Wed, 30 Jun 2021 16:24:38 +0200, "J.O. Aho" <us...@example.net>
> wrote:
[...]
>> 2. Rename the .html file to .php
>
> Uh oh. But I'm reading that I can embed php into a html file. Isn't
> that possible?

It is - but the file must be processed by PHP. This usually does not
happen, when the file exension is ".html".

Arno Welzel

unread,
Jul 1, 2021, 6:35:31 AM7/1/21
to
Not_even...@example.net:

> On Wed, 30 Jun 2021 22:23:32 +0200, "J.O. Aho" <us...@example.net>
> Thanks for clarifying that. I don't control the server, but I've read
> that those who do can allow html files to be scanned for php code.
> Maybe my Web host has in fact done that. Would I be able to find some
> notation about that in the Web host's phpinfo.php file?
>
> Also, some scripts seem to work in html files and some don't.
>
> The following is the entire code for a file I named hello_1.html
>
> <html>
> <head>
> <title>PHP Test</title>
> </head>
> <body>
> <?php echo '<p>Hello World</p>'; ?>
> </body>
> </html>
>
> It produced the result:
>
> "Hello World
>
> '; ?> "
>
> in the browser. It's obviously got a little problem in the output, but
> it did produce a mostly useful output.

No. The output comes from the fact, that PHP was *not* interpreted.

The browser displays *exactly* this:

<?php echo '<p>Hello World</p>'; ?>

Just have a look at the source of the page which is displayed by the
browser.

Since <?php will treated by the browser as the beginning of a tag, it
will be omitted in the visible output. But "; ?>" is clearly left over
as the PHP code was *not* interpreted but just sent to the browser. The
result is then what you see.

If renaming everything from .html to .php you should look for a hosting
service which allows using .html with PHP as well. Technically this is
just a configuration in the webserver - however that is usually not
possible in shared hosting environments.

Pierre Jelenc

unread,
Jul 1, 2021, 5:36:07 PM7/1/21
to
In article <4mcqdgtiqjshpeg88...@4ax.com>,
<Not_even...@example.net> wrote:
>
>Thanks for clarifying that. I don't control the server, but I've read
>that those who do can allow html files to be scanned for php code.
>Maybe my Web host has in fact done that. Would I be able to find some
>notation about that in the Web host's phpinfo.php file?

You don't need to control the server itself; many hosts --indeed all the
good ones, I say-- allow you to redirect .html to the PHP engine one way
or another, typically from .htaccess with AddType and AddHandler
directives. Check your host's help system or file a support ticket, they
probably have a FAQ answer ready.

Pierre


--
Pierre Jelenc
The Gigometer www.gigometer.com
The NYC Beer Guide www.nycbeer.org

😉 Good Guy 😉

unread,
Jul 1, 2021, 5:55:22 PM7/1/21
to
On 01/07/2021 05:49, Not_even...@example.net wrote:

It produced the result:

"Hello World

'; ?> "

in the browser. It's obviously got a little problem in the output, but
it did produce a mostly useful output.

If you have uploaded the file in the correct folder of your webhost then it should produce a clean output. It should just say:

Hello World
There should not be any other characters.

Are you sure your webhost is allowing you to run php on the server? Some servers have a basic subscription that only allows you static files - HTML/CSS/JavaScript

Who is your host? I suspect, you are running your own webserver and so you haven't configured it correctly to serve php files. We get newbies who think they have done what is required but "nothing works". They become very defensive when you try to point out their errors.




--

With over 1.3 billion devices now running Windows 10, customer satisfaction is higher than any previous version of windows.

Pierre Jelenc

unread,
Jul 2, 2021, 7:02:54 PM7/2/21
to
In article <lb4vdg1uqp8p7hhir...@4ax.com>,
<Not_even...@example.net> wrote:
>On Thu, 1 Jul 2021 21:36:05 -0000 (UTC), rc...@panix.com (Pierre
>Jelenc) wrote:
>
>>In article <4mcqdgtiqjshpeg88...@4ax.com>,
>> <Not_even...@example.net> wrote:
>>>
>>>Thanks for clarifying that. I don't control the server, but I've read
>>>that those who do can allow html files to be scanned for php code.
>>>Maybe my Web host has in fact done that. Would I be able to find some
>>>notation about that in the Web host's phpinfo.php file?
>>
>>You don't need to control the server itself; many hosts --indeed all the
>>good ones, I say-- allow you to redirect .html to the PHP engine one way
>>or another, typically from .htaccess with AddType and AddHandler
>>directives. Check your host's help system or file a support ticket, they
>>probably have a FAQ answer ready.
>
>I have apparently gotten the server to process PHP scripts inside the
>HTML file. Things is, that's kind of wasteful and may slow things
>down, for me and for others. I did read on one of the many Web pages
>I've been looking at over this topic that besides putting "AddType
>application/x-httpd-php .html" into my .htaccess file, and making ALL
>HTML files get processed for any PHP scripts, that there was also a
>way to do it for a single file. I have about five files I need to do
>this modification notice for.
>
>The code for a single file that I saw was "<Files yourpage.html>
>AddType application/x-httpd-php .html </Files>" where yourpage.html
>was whatever that one file was named.
>
>Does anyone know if it's possible to use that declaration, or
>something like it, in the .htaccess file so that only the five HTML
>files that really need to have PHP processed will get processed and
>all other HTML files will not be?

It should work (but again, it depends on the way the server is set up).
Alternatively, rename the relevant files to .htm and redirect .htm to PHP
but leave .html alone.

John-Paul Stewart

unread,
Jul 2, 2021, 8:01:17 PM7/2/21
to
On 2021-07-02 7:36 p.m., Not_even...@example.net wrote:
>> echo "" .date("Y-m-d H:m",filemtime($pfile));
>
> No matter what I do I always get the same minutes.

You're not showing minutes at all. Lower case m is month number (as you
used in the date). You probably want H:i for hour and minutes; hour
and month is an odd pairing!

https://www.php.net/manual/en/datetime.format.php

Arno Welzel

unread,
Jul 3, 2021, 9:40:36 AM7/3/21
to
Not_even...@example.net:

[...]
> <?php
> date_default_timezone_set('America/New_York');
> $file = $_SERVER["SCRIPT_NAME"];
> $break = Explode('/', $file);

The function name is "explode()" in lower case and not "Explode()". Even
if PHP accepts the upper case version as well you should use the correct
case to avoid problems with future updates.

Also see: <https://www.php.net/manual/en/function.explode.php>

Blue Hat

unread,
Aug 8, 2021, 2:43:24 PM8/8/21
to
Not_even...@example.net Wrote in message:r
> I don't even know if I'm up to the level of being a newbie at thispoint. I mostly need to do a specific thing, get a html file todisplay its last modified date and time inside itself.I'm not sure a book is going to be that useful, but maybe a Web sitecould be. I'd need one that really is basic.I've found a Web page or two, but they seem to assume that I know morethan I do because I can't get the code snippets I've found on thoseWeb pages to work.I used to use ssi for this, but I moved to a new Web host and theyseem to have their server time zone set as the only time zone I candisplay, and they are far away from me, so in ssi I get a date andtime for a file modification time that's many hours off. I'm hopingthat php will solve my problem. Thanks in advance.

--


Have you tried setting up your own webserver to try this out on?
Try doing basic stuff and work your way up to figuring it out? I
had messaged similar problem with using JS in PHP and although my
solution wasn't too elegant it did what I wanted.


The point is to break down what u want done and attack each step.
Next, use HTML in your file to do what you want to then replace
each one with a PHP snippet. I could offer you some help if you
would like.


----Android NewsGroup Reader----
https://piaohong.s3-us-west-2.amazonaws.com/usenet/index.html
0 new messages