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

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This sounds like there is a "backstory" to this question.  If we understood that, we might be able to give you a better answer.  PHP usually runs as "nobody" or something similar.  Nobody changes the identity of PHP.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Nathan RileyFounderAuthor Commented:
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dave BaldwinFixer of ProblemsCommented:
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.
0
Nathan RileyFounderAuthor Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Nathan RileyFounderAuthor Commented:
Ah got it so the submit file I hit should be on the other domain.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Nathan RileyFounderAuthor Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
Then you are probably running into Apache security issues as well.  A separate domain under a different web root will be isolated by Apache security.  That means that although you might be able to access a file on another domain using a complete URL 'http://domain2.com/contact.php', you won't be able to access it with a relative URL like 'contact.php'.

You probably want to rethink this plan.  If you don't need the isolation that separate domains would give, I would put it all under one web root so you don't have any cross-domain or cross-user problems.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gheistCommented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.