Closed Thread Icon

Topic awaiting preservation: Finding OS in PHP Pages that link to <a href="https://ozoneasylum.com/backlink?for=12092" title="Pages that link to Topic awaiting preservation: Finding OS in PHP" rel="nofollow" >Topic awaiting preservation: Finding OS in PHP\

 
Author Thread
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 03-07-2002 23:38

I'm using a flat file database to store content for a site that is input from an admin section. Just to be safe, I have used file locking on the files when they are being written to, but file locking doesn't work in windows, which is the OS I do my coding on.

To get to the point, I want my script to be able to tell which OS it's running on, or at least when it's on windows so I can do an if else to include the file locking function or not depending on OS. I've come up with this and it seems to work, but I know there's got to be a better, more accepted way to do this.


if (!@getrusage())
{
$os = "Windows";
}
else
{
$os = "Linux";
}

I thank you kindly for any and all enlightenment.

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-07-2002 23:53

Yup it's in your environment vars

HTTP_ENV_VARS["OSTYPE"]

Should return the OS of the Server.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 03-08-2002 01:33

Thanks Bit

I don't know why I didn't see it the first time through my phpinfo.php file.

And just in case somebody as backward as me looks at this thread and needs the same thing, it's

$HTTP_ENV_VARS["OS"]

Thanks again Bitdamaged


-Butcher-

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 03-08-2002 02:43

Stay tuned for more after these important messages!

Ughhhh!

Could someone please tell me why this doesn't work?

<?php
function working_folder()
{
$os = $HTTP_ENV_VARS["OS"];
if (preg_match("/windows/i", $os))
{
//
preg_match("/[^\\\]+$/", getcwd(), $match);
$working_folder = $match[0];

} else {

preg_match("/[^\\/]+$/", getcwd(), $match);
$working_folder = $match[0];
}
return $working_folder;
}

?>

If I just comment out one or the other preg_match without using the if else, it works fine, but I don't want to have to do that everytime I switch OS's. I don't get what's wrong.

Thanks

-Butcher-

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 03-08-2002 07:55

Your code from above doesn't work, because you didn't define $HTTP_ENV_VARS as global inside working_folder() function...

Anyway, there's a much easier way to find out OS on which PHP is running. Just check PHP_OS predefined constant, which will return either WIN32 or WINNT when running under Windows...

function working_folder()
{
&nbsp;&nbsp;&nbsp;&nbsp;preg_match("/[^\\" . ((substr(PHP_OS, 0, 3) == "WIN") ? "\\" : "/") . "]+$/", getcwd(), $match);
&nbsp;&nbsp;&nbsp;&nbsp;return $match[0];
}


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 03-08-2002 22:44

You're unbelievable mr.maX

Thanks!

-Butcher-

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 03-09-2002 01:03

I would like to apologize to Bitdamaged for making it seem like he made an incorrect statement earlier in this thread.

HTTP_ENV_VARS["OSTYPE"] is a correct answer to my original question, it just isn't listed that way in my particular set up on my server. It is listed that way on other servers though. I don't know if it's a version thing or an OS thing, but that's the deal.

Sorry Bitdamaged


-Butcher-

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 03-09-2002 09:35

Environment variables are OS dependent (that's why they have "environment" in their name), and that's the reason why OS variable is not the same on your computer and on web server...


« BackwardsOnwards »

Show Forum Drop Down Menu