Jump to content

Recommended Posts

I have an up and running website that I want to completely revamp so I figured I would work on it locally then upload the new site when it is finished. Since I have never done this before it brings up questions on how exactly to do that.

 

I downloaded and installed WAMPserver 2.2 and everything is working fine. I copied the site into the www folder and now the problems start :) yay! I am already having to change many things in my code for the working site to get it to run as localhost.

 

How does a person go about writing code that works both as localhost and in the domain when uploaded? It seems to me that when the site is configured to work as one or the other there will be a lot of little things that won't work such as links, or sessions and such. I figured out how to make links work properly by using variables in the link so all I have to do is change the variable value from (for example) mysite.com to localhost/mysite

 

My fear is that after I spend a month doing this that it will be a nightmare converting it over to work in the domain on a different server. I'm sure there are secrets and things that make this easier and I could use some help finding out how to best go about this.

 

Thanks!

For the last couple of years I ask very few questions but I always end up answering them myself, and this is no exception. I am using:

$serverName = $_SERVER['SERVER_NAME'];

and then checking the result to see whether the page is running local or in a domain and then writing the values of the variables accordingly so the page will work local and also when uploaded.

There should be no difference to the script whether it runs on a web server on your local computer, or any other computer. If there is, then chances are high that you've overcomplicated the matter, and could benefit from reducing the complexity.

This may help and it's how I do it

 

// find out if url has the word local in it and if the ip matches my local ip
if (stristr($_SERVER['HTTP_HOST'], 'local') || (substr($_SERVER['HTTP_HOST'], 0,7) == '127.0.0')) {
$local = TRUE;
$home = 'http://localhost/mywebsite';
}  else {
$local = FALSE;
$home = 'http://www.mywebsite.com';
}

 

Then links are echo '<a href="$home">Home</a>'; or what ever.

 

Like ChristianF said there should be almost no difference.

 

With the above I can move files back and forth with no problem.

 

 

There should be no difference to the script whether it runs on a web server on your local computer, or any other computer. If there is, then chances are high that you've overcomplicated the matter, and could benefit from reducing the complexity.

 

Hmmm, you must know something I don't because I have to do things like this:

 

    $serverName = $_SERVER['SERVER_NAME'];
    $serverNameParts = explode('.', $serverName);
    if(count($serverNameParts) == 1) {
		$change_site = "localhost/website";
    } else {
		$change_site = "website.com";
    }
$domain = "." . $serverName;
session_set_cookie_params(120 * 60, '/', $domain);
session_start();

 

and then for links that is where the $change_site variable comes into play:

 

if(!isset($_SESSION['userInfo'])){
echo "<script language='JavaScript'>window.location='http://" . $change_site . "/log_in.php?return_link=" . $return_link . "';</script>";
}

 

 

How I develop on a "localhost" to avoid the issues you are seeing is setup your own "domain" via the windows hosts file.

 

127.0.0.1    domain.dev

 

By doing this, you are "simulating" the real webserver so that the transfer should be transparent. I know localhost has weird issues, especially with cookies etc. So that should resolve that part of the problem. Just remember to also change the Apache file so it matches that domain locally so it works properly. And in doing this, you can enable your webserver to serve multiple projects sites without having to always switch the "localhost" one. 

I am already having to change many things in my code for the working site to get it to run as localhost.

 

What are some examples of the kind of stuff you're having to change?

 

Well I think most things I am having to change result from going from one version of php/mysql to another the WAMPserver has older versions that the server I am used to dealing with so I keep getting a lot of undefined function errors that require some code tweaks to eliminate. After dealing with this for a day and a half now I think it isn't as bad as I first thought it was going to be. Well until I upload it and go back to a different version of php/mysql that is. I suppose I could upgrade the environment on the WAMPserver.

How I develop on a "localhost" to avoid the issues you are seeing is setup your own "domain" via the windows hosts file.

 

127.0.0.1    domain.dev

 

By doing this, you are "simulating" the real webserver so that the transfer should be transparent. I know localhost has weird issues, especially with cookies etc. So that should resolve that part of the problem. Just remember to also change the Apache file so it matches that domain locally so it works properly. And in doing this, you can enable your webserver to serve multiple projects sites without having to always switch the "localhost" one.

 

Thank you for that information, I'll look into that.

The only one I can remember right off the top of my head was for my site key on the log in page. It worked fine on the server, but in localhost I had to put quotation marks around the key for it to clear the error.

It worked fine on the server, but in localhost I had to put quotation marks around the key for it to clear the error.

 

The code was also producing that error on the server, but the error was being hidden by the error_reporting/display_errors settings. Php was still detecting that problem every time the code runs, but it was not being reported and/or displayed and if php was actually logging the error to the error log file, you are gradually building a humongous error log that makes it difficult to find actual errors that might be occurring when your code runs.

 

All code should not produce any kind of errors during its normal execution. Only for abnormal things, such as a legitimate visitor feeding your script unexpected values that you didn't take into account or a hacker trying to break into your script, so that you can find and fix actual problems.

 

Unless you are trying to develop a general purpose script that would work on a minimum php/mysql version, your development system should have the same version of php/mysql as your live server so that you don't waste time making changed just due to the version differences.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.