Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting SQL Login/pass from another file. 2

Status
Not open for further replies.

Mudassar

Programmer
Oct 3, 2000
110
AU
I'm a newbie in PHP SQL.

Say if this was my code in a php file:

<?php
$Host = "127.0.0.1";
$User = "usernm";
$Password = "passworsde";
$DBName = "mydatabser";

This code is in every PHP page that I have created. (At least 35)

Is there a way just to put that above code in a seperate file and just use a get command or somthing to "get" the variables and its data??

Would make it heaps quicker say if the password was changed. All I have to do is edit 1 file instead of 35.

-----------------
MK
 
What i do is create a file called config.php. In this i have my mysql connections and other functions.

CONFIG
Code:
<?php
$Host = "127.0.0.1";
$User = "usernm";
$Password = "passworsde";
$DBName = "mydatabser"; 
?>

Then on other pages i put this at the top:
Code:
<?php
include "config.php";
?>

hope it helps!


Regards,

Martin

Gaming Help And Info:
 
Thanks for your response.

I have tried what you said, It seems to find the config.php file OK, but it can't seem to get the variables.

Example:

config.php:

<?php
$Host = "127.0.0.1";
$User = "usernm";
$Password = "passworsde";
$DBName = "mydatabser";

print ("Host = $Host <BR>");
?>


index.php:
<?php
include "config.php";

print ("Host = $Host");

?>


Execute index.php
OUTPUT:
Host = 127.0.0.1
Host =

Any reasons why this is happening to me?





-----------------
MK
 
Page1:
Code:
<?php
$Host = "127.0.0.1";
$User = "usernm";
$Password = "passworsde";
$DBName = "mydatabser"; 
?>

Page2:
Code:
<?php
require("page1.php");

echo "{$User}@{$Host}";
?>

After connected, remember to unset the variables that are sensitive..

You can also run something like this:
* run code in the page1.php that checks filename. If filename is the connection file, unset all variables.

you might also want to chmod the dir. that contains the db_conn.php file.. (connectivity information).

Some times, I have made functions that wrap around connection functions.. eg. a function that connects, runs query, etc.

Olav Alexander Mjelde
Admin & Webmaster
 
output:

@


Strange isn't it. Maybe there’s a security setting that’s preventing access to external variables.
I am very sure that it’s connecting to the included file fine. Just doesn't like to retrieve any data from it.

-----------------
MK
 
I know its less security, but is there a HTML Include command that I could use?

-----------------
MK
 
As PHP is SERVER SIDE, you cannot include on the client!
eg. after the server has processed the files, there are no variables, or anything.

ps. use require() instead of include() on vital files, as then it will halt the script, if the file is not working for some reason.

I dont know why your variables dis-appear, try echoing them in the first php file?

Olav Alexander Mjelde
Admin & Webmaster
 
I added the line
<code>
echo "CONFIG file:{$User}@{$Host}";
</code>
On the CONFIG.php file AND


<code>
include "config.cfg";
echo "INDEX file:{$User}@{$Host}";
</code>
on the INDEX.PHP

I ran the INDEX.PHP and it displayed the following result:

CONFIG file:usernm@passworde
INDEX file:mad:

I'll try another server.

-----------------
MK
 
try on config file:

Code:
global $Host;
global $User;
..

//set vars
$Host = "127.0.0.1";
$User = "usernm";
on main file:
Code:
echo "Server Host: ".$Host;

PS. if its a local address, i use "localhost" not "127.0.0.1"

Regards,

Martin

Gaming Help And Info:
 
Thanks for your help so far guys!

I figured out what I was doing wrong:

When I was using the include command I typed in the entire address like so:


Even though it finds the file, it doesn't send variables.

But when I typed

include/config.cfg

it worked!

But now heres the problem. What if this file was located in another server and I do need to use the full http address. Is there a way around this?

-----------------
MK
 
You do NOT want it to be able to send the password and username via another server!

Why not?

It's a really bad idea, regarding to security.

You can however, make multiple users in your mySQL database, and assign them with different rights to different databases.

Also, As I said above: require() on vital files, not include().

Olav Alexander Mjelde
Admin & Webmaster
 
OK then. Thanks alot DaButcher and MJB3K!


-----------------
MK
 
np. Good luck :)

ps. if you visit this forum on a regular basis, you can learn by teaching!

I find that if I have some spare time, where I'm kindof bored, it's nice to check if someone here needs help that I can help them with.

Olav Alexander Mjelde
Admin & Webmaster
 
That's why it's always a good idea to post your *exact* code and use the sample code *exactly*. When you paraphrase or interpret you have the risk of hiding the actual problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top