Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Run PHP Script as Different User

I have a php script that writes to a file.

How can I change to a different user that I want to execute the command?

<?php
$f = fopen("test.txt", "a+");
fwrite($f, "
blah");
fclose($f);
?>

Open in new window

SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
If you are running PHP in the default location which is owned by 'root', then you are usually running under Apache's user which is intentionally very limited.  If you are running in your own user directory, usually in 'public_html', then you can use 'suexec' to run as your own user.  Running as any other user but those two is pretty unlikely.
Avatar of Nathan Riley

ASKER

No real backstory.  I have a script that resides under one account that I need to modify a file under another account/domain that's all on the same server.  When it runs now it says access denied, so assumed I need to run it as the other account with a username and password?
That is what it means but PHP and Apache are not allowed to do that in a normal installation.  Are you running this on the command line instead of thru a web page?  Then you could see what is allowed by using 'gedit' to open and try to save the file.  You should get the same results because of permissions.
Hmm...well maybe my method of doing the entire process is flawed.  I wasn't sure how to accomplish so I'll post it here and see if you can think of a better way, it's a little lengthy.

I have a web app that I'm trying to somewhat create custom registration URLs for so that the user registering gets registered to the proper organization.  The base registration URL is connect.domain.com

The admin accounts are setup via an admin page on a different domain(basically an umbrella domain for many products) where I type in a few things like email, organization name etc...it submits to a .php file creates the user in that database and we store it.  The end registration url is connect.domain.com?orgID=2442  At the time of creation I store this data.  At the end of my script I'm trying to have it add a line to the end of the .htaccess file where it does a rewrite to connect.domain.com/organizationName this way it's cleaner and somewhat customized.

Let me know if you have questions on the above hope I haven't confused.  Open to doing this differently, I really didn't know where to start so ran with what I thought would work.
Things like that are normally done under the same user on the web site.  You can give them their 'own' directory inside the web root but you will not normally have access to anywhere else or any other user on the system.  Most sites with URLs like connect.domain.com?orgID=2442 use 'index.php' as a router or dispatcher to send the user where they need to be.
Ah got it so the submit file I hit should be on the other domain.
If they are both under the same 'web root', it doesn't matter.  Under the same hosting account they both have access to the same resources.

In the setups I've seen 'connect.domain.com' is a subdirectory under 'domain.com'.  That's the way subdomains are structured in my web hosting accounts.
Ah, I see.

Well this is setup as domain1.com is the umbrella where I'm executing the script that sets up the admin accounts for sub products that are their own entities on domain2.com domain3.com etc...all hosted on the same server.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Indeed mod_suexec does that together with mod_fcgid (config is not trivial to put together from crumbles of documentation, but config change should fit in one 25x80 screen)