Jump to content

PHP - Tips when working on live websites

Drown

Hello everyone,

 

I'm working on several project at work and a lot of them are already online. The code is pre-existent, and it's all set for production. It's very painful to do some work on these websites, everytime I make a change I have to re-upload them on the server - it gets very tedious when working with CSS.

 

For smaller projects this isn't really an issue, I can go through the 3-4 pages and change the links manually. However, when working on the bigger websites with several dozen of pages, and thousands of lines of code, that's not really a possibility. I guess I could go through the code and change the paths, but then if I do some major changes, I'll have to change everything again to drop it on the server.

I was wondering if you guys had any tips on the best practices to use in these situations. Is there some sort of software, like a PHP "builder", some sort of preprocessor that would go through my code and change the paths for me, based on a config or something?

 

For new projects I'm thinking ahead and using a constant in my paths, so when it's time to go in production I can just change one constant and pretty much everything will work. Implementing this for the bigger projects would be possible I suppose, but very time consuming, so I'm wondering if there's something out there that can help me.

 

If not, too bad, I'll just deal with it as it is, and make sure that I don't do the same mistakes as my predecessor in other projects. 

I would be interested in seeing what are your approaches in these situtations.

Thanks all for the help. :)

 
Link to comment
Share on other sites

Link to post
Share on other sites

Use WinSCP with sftp protocol, it will automatically upload file when you save it in your editor. You just opening files without downloading them WinSCP will download it by itself.

 

Edit:

With those paths, you would need to make example for me, but if you create links make them relative than absolute, it is: without domain so after you move your site to another domain, links will still work like /index.php instead of http://www.example.com/index.php

 

If you need to change something in many files you can download whole site on local and then find and replace in those files.

Link to comment
Share on other sites

Link to post
Share on other sites

Use WinSCP with sftp protocol, it will automatically upload file when you save it in your editor. You just opening files without downloading them WinSCP will download it by itself.

 

Thanks, I'll definitely give this a shot!

 

However, my original question still stands, while I'm mostly working on "closed" sections of the website - where it doesn't matter if I break stuff and do a lot of experimenting - it's not the case for all the projects, and sometimes I have to work in local.

Link to comment
Share on other sites

Link to post
Share on other sites

Well, a good IDE will let you search all files for a string and replace them. For example, in dreamweaver you can use the find function (ctrl+f) and search through the whole site for your link. You can also replace them all. Like so

 

bc41632ae0.png

 

I believe a few others also support this.

 

Alternatively, you CAN make a php file that will do all of that. You would need to use standard file methods, as well as glob() and strpos() and what not

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Using relative paths for internal links is the easiest solution.
 
Here's what I do:

<?php// Determine whether the page is served over HTTPSfunction is_secure() {  return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' || $_SERVER['SERVER_PORT'] == 443);}// Get the correct protocolfunction protocol() {  return (is_secure()) ? 'https' : 'http';}// Get the base URL of the site, e.g. linustechtips.comfunction base_url() {  static $base_url;  if (!isset($base_url)) {    $base_url = protocol() . '://' . $_SERVER['HTTP_HOST'];  }  return $base_url;}// Get the base path of the site, for example if the site is not installed at the server root// but is instead in a subdirectory, e.g. linustechtips.com/main would return "main"function base_path() {  static $base_path;  if (!isset($base_path)) {    $base_path = $_SERVER['SCRIPT_NAME'];    $base_path = explode('/', $base_path);    array_pop($base_path);    $base_path = implode('/', $base_path);  }  return $base_path;}// Construct an absolute URL to a resource, given the relative path to that resourcefunction url() {  $argv = func_get_args();  $url = base_url() . base_path();  foreach ($argv as $arg) {    $url .= '/' . $arg;  }  return $url;}

To create a path on a page:

<a href="<?php echo url('path', 'to', 'some', 'page') ?>">Link text</a>

Outputs:

<a href="http://www.domain.com/path/to/some/page">Link text</a>
Link to comment
Share on other sites

Link to post
Share on other sites

As others have mentioned the easiest way to ensure your links work is to use relative paths, then you never need to worry about breaking them.

 

However eventually you may need to change other things based on the environment such as database connections, error and warning message visibility, api access, etc.

 

I use Zend so to manage this you can create an application.ini file that will use different settings for different environments so the same code can be deployed everywhere without any changes and have the settings set suitably like this:

[production]phpSettings.display_startup_errors = 0phpSettings.display_errors = 0includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" [staging : production] [testing : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1 [development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1

Code Source: http://framework.zend.com/manual/1.12/en/zend.application.quick-start.html

 

EDIT: Also rather than re-uploading your work (via FTP I guess?) you might want to look into something like Git or GitHub if its easier. Again with my work I can simply tag the commited code I want to deploy with a version number (git tag -a "v1.2.3") and then its just a case of checking out that code on the other environment either with a dedicated release tool which I do or using "git checkout tags/v1.2.3"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×