Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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!

share|improve this question
    
I don't understand your queston clearly. Are you talking about not having to copy+paste/manually code everything on every page? –  Terence Ponce Oct 14 '10 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 '10 at 7:03
1  
Jeff O's answer pretty much sums it up. –  Terence Ponce Oct 14 '10 at 7:37
add comment

6 Answers

up vote 5 down vote accepted

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.

share|improve this answer
1  
This answer explains Jeff O's answer, regarding database classes, further: stackoverflow.com/questions/2035449/why-is-oop-hard-for-me/… –  Terence Ponce Oct 14 '10 at 7:42
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
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. –  jcomeau_ictx Nov 23 '10 at 1:48
add comment

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

share|improve this answer
    
How would that go? –  Trufa Oct 14 '10 at 13:28
add comment

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.

share|improve this answer
add comment

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..."

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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