5

I am using MAMP (just and example) to develop web applications without having to constantly upload and download via FTP.

I´m sure this is the correct way to test a project but I´m no sure if I´m doing it right.

After I´m satisfied with the project I have to change in every page the MySQL databases and most of the links etc.

Is there a workaround this? How should this be used?

Thanks in advance!

3
  • I don't understand your queston clearly. Are you talking about not having to copy+paste/manually code everything on every page? Oct 14, 2010 at 7:00
  • 1
    No, I´m talking about having to modify the links from: localhost:8888/test to example.com/test or having to change mysql_connect("localhost", "root", "root") to the actual password etc. Do I make myself clearer?
    – Trufa
    Oct 14, 2010 at 7:03
  • 1
    Jeff O's answer pretty much sums it up. Oct 14, 2010 at 7:37

6 Answers 6

6

Whether it is local or not, you need a development environment outside of production.

I'm no PHP expert, but every page should not have its own connection code to the database. Create a class that can be reused. Your application should be able to determine if you are on local host or example.com and connect to the appropriate database.

And for hyperlinks, look into Relative URL instead of Absolute URL.

1
3

It's always better to have a common class which contains your database connect, database disconnect and your most used paths like

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];
$config['base_path']= $_SERVER['DOCUMENT_ROOT'].'/project/';

These are very help full.

1

Instead of hard coding the links, use a variable to hold the base URL, then append that as a prefix.

1
  • but only if the base URL is necessary. in many cases it isn't; using "img src=/images/bleah.png" will always work to locate a file from the DocumentRoot. Nov 23, 2010 at 1:48
1

I'd have a configuration file with both the database connection info AND the server URL, you could actually use

define('SERVER_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/');

It's what I use when I work with WordPress :)

1
  • How would that go?
    – Trufa
    Oct 14, 2010 at 13:28
0

I Would have a configuration file, which contains the bits that you have to change every time, so that you will only have to make a few changes.

0

Yet another way, in the very unlikely case that none of the above suggestions works, is to put a line in your hosts file (/etc/hosts on Unix or \windows\system32\drivers\etc\hosts on Windows) such as:


127.0.0.100 my.server.com  # or any number in the 127/8 network.

Just don't forget to remove it, or comment it out with a preceding "#", when you actually want to connect to my.server.com. It can be very confusing, annoying, and even dangerous if you forget. "Oh no, how did all my private data end up here on the server??? rm -rf private/. oops..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.