Tips , Mini Course PHP , PHP
- 29/12/2008
Mini Course on PHP - news System (tip)
Well folks, here was putting the system of news in the air, or rather the part that we have already made the news system. And remember to make a change to banco.php. The data reported in this file to access the database. But now I must leave the system running on my machine and online. I have a problem: The file banco.php on my machine will be different banco.php file that will be online. As I have had many problems with it, I always half-kludge.
In this half-workaround we use a super-global called $ _SERVER. This is a super-global array that contains various information.
We will use the option 'HTTP_HOST'. This option returns the domain name that the script is running. Run the following script into your browser:
- <?
- [ 'HTTP_HOST' ] ; // mais info em: http://br.php.net/manual/pt_BR/reserved.variables.server.php echo $ _SERVER ['HTTP_HOST'] / / more info at: http://br.php.net/manual/pt_BR/reserved.variables.server.php
- ?>
Note that it will print: localhost
In my case, if I run here on the site, will be printed: www.marcelosabadinil.com.br
So I made some changes to the file banco.php. Below oficou with the source:
- <?
- $_SERVER [ "HTTP_HOST" ] == "localhost" ) { // aqui se o script rodar em localhost if ($ _SERVER ["HTTP_HOST"] == "localhost") {/ / here is the script run on localhost
- ; // HOST para conexão $ Host = 'localhost' / / HOST for connecting
- ; // usuário $ User = 'root', / / user
- ; // senha $ Pass = "/ / Password
- ; $ Db = 'sistema_noticias';
- { // se rodar em outro servidor, no meu caso é o marcelosabadini.com.br, vc pode colocar um ELSE IF aqui caso seu script rode em mais de dois servidores heehehehe… é meio improvavel, mas… Else {} / / if run on another server, in my case is marcelosabadini.com.br, you can place a ELSE IF here if your script runs on more than two servers heehehehe ... it's kind of unlikely, but ...
- ; // HOST para conexão $ Host = 'mysql.marcelosabadini.com.br' / / HOST for connecting
- ; // usuário $ User = 'XXXXXXXX' / / user
- ; // senha $ Pass = 'XXXXXXXX', / / password
- ; $ Db = 'database_name';
- }
- # === - Here he tries to make the connection to the database
- # === - If the connection succeeds
- $con ) { if ($ con) {
- # === - Select the database
- { Else {}
- . mysql_errno ( ) . ') ' . mysql_error ( ) ; echo 'OPS ... I think an error has occured, see error below: <BR> ('. mysql_errno (). ')'. mysql_error ();
- }
- ?>
Note that the first section of the script is just an IF that tests where the script is running. If it's on localhost, then the data access to the database are X, otherwise the data is Y.
Now files that are on the server are exactly identical to the files on my machine, I can give a Syncronize with Dreamweaver without worrying about the file banco.php.
Well, that's one more tip I leave to you. Hope you enjoy.



2 Responses to "Mini Course PHP - System news (hint)"
I usually define a constant at the beginning of the script, so just give a constant in IF anywhere in the code.
define ('LOCALHOST' ($ _SERVER ['SERVER_ADDR'] == '127 .0.0.1 '));
Hugs ...
By Marcelo (3 comments) on 25/01/2009
Hello Marcelo everything good?
Surely this is so very interesting and I recommend it (it's basically how I use)!
But, I do it in a function.
If anyone reading this who do not understand the line posted by my Chara, please leave a comment!
Hugs!
By Marcelo Sabadini (109 comments) in 25/01/2009