Forward Request from one PHP page to another


I am a java developer trying to learn PHP and I am struggling leaning how to forward a request from one PHP page to another. Maybe I just need to forward from one page to another without the request but with Java I always forward the request.

In Java, I would have a form post to a servlet. The servlet would then call a Bean to execute some business logic and then forward the request to a JSP page.

I am trying to mirror this logic in PHP.

Here is how my code is setup.

+-- lib
      +-- control
            |-- insertData.php
      +-- dao
            |-- IDataDao.php
            |-- MySqlDataDaoImpl.php
      +-- logic
            |-- DataBean.php
      +-- model
            |-- DataValueObject

On my index.php page I have an “Add Data” link. Clicking on this link, calls insertData.php, which instantiates a DataBean and executes the insertData function. This function inserts the data using the implementation MySqlDataDaoImpl.php of the interface IDataDao.php.

This works fine for me. Here is my problem, I now want to direct back to the index.php page from insertData.php.

I have Googled forwarding (redirecting) a page in PHP but I do not want to hard code the URL since I would need to change this value between my development environment and my hosting environment.

header(“Location: http://domain/page.php”);

In insertData.php, how would I direct the user back to the index.php page without hard coding the location?

Thanks in advance for your time and knowledge.

I would write some code to abstract the url path so that you can control it via a configuration file.

Btw- It’s not hard to setup a virtualhost on your local apache server. Then you can do something like http://foo/index.php
and have it all mapped to what would otherwise be http://localhost/foo/

Now, you’re only needing to change the actual domain name.

Thank you for the feedback but is this really the only way to pass control from one PHP to another? I just find it hard to believe that I have to hardcode the url path or even configure it. I guess it is possible because I have spent a few hours on Google and have not found a better approach.