mysql php安全

On my php pages, at the top i connect to the database like this

$db = mysql_connect("mysql.site.com","thedb", "pass"); 
mysql_select_db("dbase",$db); 

Is this secure? Could someone somehow scan and view my code, therefore get access to the database?

UPDATE

Reason I ask is because a user was able to get access to my database, and im pretty sure it wasn't through sql injection.

It would be better if you move this snippet to an include file outside of your document root — this will prevent people reading it in case your webserver somehow gets misconfigured and starts serving PHP files as plain text. Although, just by itself, it is secure enough — it is unlikely that somebody will be able to misconfigure your server like this on purpose.

If someone did have access to your code, then yes, they would be able to read the password out.

There isn't a huge amount you can do about this, but ensuring that this code is a directory up from your web root would help.

(i.e. if you are serving your site from the folder /usr/htdocs/mysite, change it to /usr/htdocs/mysite/public, then put your includes in mysite rather than public.)

That's ok, the important thing I could say, go to your database and give to that user restricted permission, I mean only select, insert , update and delete the tables that it need.. beside that, create a file with that info and just include when you need it, that's my advice.

If someone go through your code will be able to see that info, but try to reduce always the damage impact

You should always apply multiple layers of defense:

  1. Remove the credentials from the source code and place them outside the web server’s document root.
  2. Restrict access to your database, possibly only via localhost or via socket.
  3. Restrict the user’s privileges to only those necessary.