Forum Moderators: phranque

Message Too Old, No Replies

Accessing TIME variables

         

csdude55

9:11 pm on Dec 10, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I see that Apache has several TIME variables:

[httpd.apache.org...]

(listed under "Misc variables")

I've been creating this same information in PHP using date(), so it would theoretically be faster if I could access these server variables instead. But print_r($_SERVER); doesn't include them!

Is this something that I have to activate in Apache in some way?

Or is there a different way to access them in PHP?

phranque

10:05 pm on Dec 10, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



have you tried the apache_getenv [php.net] function?

csdude55

1:35 am on Dec 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I had not, but just did:

<?php
$time = apache_getenv('TIME');
echo $time;
?>

That gives me a 500 ISE:

Call to undefined function apache_getenv()

Tonight I'll see if I can convert them to environment variables using:

RewriteRule ^ - [E=current_date_and_time:%{TIME},E=today:%{TIME_YEAR}%{TIME_MON}%{TIME_DAY},E=this_year:%{TIME_YEAR}]

that I could then access using $_SERVER['today'] or whatever.

I have to try that late, though, in case it causes a server wide error.

not2easy

3:58 am on Dec 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I do not believe you can use PHP in htaccess rewrite rules. I don't see how that would be a thing.

phranque

5:47 am on Dec 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I do not believe you can use PHP in htaccess rewrite rules.

this method would actually set an environment variable in the "apache process" that is subsequently retrieved in the "PHP subprocess".

csdude55

6:14 am on Dec 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think we're going opposite directions, @not2easy. I was trying to find a way to access these specific Apache rules in PHP, not the other way around :-)

I just tried the rules above, though, and it did work!

RewriteRule ^ - [E=current_date_and_time:%{TIME},E=today:%{TIME_YEAR}%{TIME_MON}%{TIME_DAY},E=this_year:%{TIME_YEAR}]

I'm not sure if there was another way to access them without rewriting them to another variable (surely there is?!), but this worked. And now I can just use $_SERVER['date_and_time'], for example, instead of using the PHP function of $date_and_time = date('YmdHis');

Perl's even better. Now I can use $ENV{'date_and_time'} instead of the cumbersome:

($sec, $min, $hour, $day, $mon, $year) = (localtime)[0..5];
$date_and_time = "%d%02d%02d%02d%02d%02d", $year + 1900, $mon + 1, $day, $hour, $min, $sec);

csdude55

7:45 pm on Dec 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For future readers, I found this today:

The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables. Note that the variables accessible in mod_rewrite without the ENV: prefix are not actually environment variables. Rather, they are variables special to mod_rewrite which cannot be accessed from other modules.

https://httpd.apache.org/docs/current/env.html#using

So it would appear that %{TIME} is NOT directly accessible outside of Apache, so there's no option but to set it to an environment variable like that.

As far as I can tell, it had no impact at all in the time it took to connect to the server, but decreased page load time by about 50ms (I only tested PHP, not Perl). Which is very small, of course, but all of my little micro-optimizations have had a cumulative impact :-) And faster page load results in more pages per session, so if it brings in more money then it was worth the time to figure it out.

[edited by: phranque at 9:28 pm (utc) on Dec 11, 2022]