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?

$f = fopen("test.txt", "a+");
fwrite($f, "

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
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.
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?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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.
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

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 submits to a .php file creates the user in that database and we store it.  The end registration url is  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 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.
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 use 'index.php' as a router or dispatcher to send the user where they need to be.
Nathan RileyFounderAuthor Commented:
Ah got it so the submit file I hit should be on the other domain.
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 '' is a subdirectory under ''.  That's the way subdomains are structured in my web hosting accounts.
Nathan RileyFounderAuthor Commented:
Ah, I see.

Well this is setup as is the umbrella where I'm executing the script that sets up the admin accounts for sub products that are their own entities on etc...all hosted on the same server.
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 '', 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.

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
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)
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

From novice to tech pro — start learning today.