0

I have three PHP files like following :

config.php

<?php
  session_start();

  $database_server = "localhost";
  $database_name="dbname";
  $database_username="root";
  $database_password="";

  $_SESSION["server"] = $database_server;
  $_SESSION["dbname"] = $database_name;
  $_SESSION["dbuser"] = $database_username;
  $_SESSION["dbpassword"] = $database_password;



?>

db.php

<?php
include("config.php");
 session_start();
  //config file for database
  $con = mysql_connect($_SESSION["server"],$_SESSION["dbuser"],$_SESSION["dbpassword"]) or die(mysql_error());
  if ($con)
  {
         $dbhandler = mysql_select_db($_SESSION["dbname"] , $con) or die(mysql_error()); 
  }
  else
  {
      echo "not possible to connect to database";
  }
?>

header.php

<?php
   include ("../config/db.php");

?>
    <div class="topbar">
        <div class="fill">

                <div class="container">
                    <h3><a href="#">title</a></h3>

                    <ul>
                        <li><a href="#">General Config</a></li>
                        <li class="active"><a href="#">Add title</a></li>
                        <li><a href="#">title</a></li>
                        <li><a target="_blank" href="http://www.site.com/">website</a></li>

                   </ul>


                </div>
            </div>
    </div>

I am developing using xampp and everytime I try to run the header.php I'll get these errors :

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 80

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 166

Notice: Undefined index: server in C:\xampp\htdocs\ossila\config\db.php on line 5

Notice: Undefined index: dbuser in C:\xampp\htdocs\ossila\config\db.php on line 5

Notice: Undefined index: dbpassword in C:\xampp\htdocs\ossila\config\db.php on line 5

Notice: Undefined index: dbname in C:\xampp\htdocs\ossila\config\db.php on line 8

To be honest I don't have any idea where is these problems comes from? I'm using xampp 1.7.7 (latest version) is it because of some misconfiguration in xampp? or should I use some other way to config the session?

6
  • 1
    Have you tried dropping the session_start() out of your db.php file? (Blind guessing) Commented Mar 28, 2012 at 13:07
  • 4
    wooooah! don't do this: $_SESSION["dbpassword"] = $database_password; Commented Mar 28, 2012 at 13:08
  • 1
    Saving db details in session not a good practise Commented Mar 28, 2012 at 13:09
  • 1
    The Deprecated messages come from PEAR and are caused by code that is there for PHP4. If you want to fix the problems, you could go an look in C:\xampp\php\PEAR\Config.php where you will find on lines 80 and 166 that there are lines that look like this: $var = &new ClassName;. All you need to do to fix this is remove the &. It won't break anything on your server, it's just there for PHP4 backwards compatibility. Or you could safely ignore it, and turn error reporting off in production (as you should anyway). Commented Mar 28, 2012 at 13:13
  • 1
    for the undefined index error, try testing the values before using it for example try if (!isset($_SESSION['dbname'])) Commented Mar 28, 2012 at 13:33

1 Answer 1

3

I see a few problems here.

  1. You shouldn't save database connexion in session. You can safely define a variable in config.php & call it in db.php as long as you include config.php in db.php

  2. Don't call session_start twice in the same process. If you want to be safe, call it at the very beginning of your PHP processing (probably the page which include header.php)

  3. You should use realpath(dirname(__FILE__)) to include files, it's safer and can avoid a lot of mistakes

Here is the config.php file :

<?php
  //session_start(); It doesn't make sense to call that in db.php, call it at the beginning of your page

  $database_server = "localhost";
  $database_name="dbname";
  $database_username="root";
  $database_password="";
?>

db.php

<?php
include(realpath(dirname(__FILE__))."/config.php");

  $con = mysql_connect($database_server,$database_username,$database_password) or die(mysql_error());
  if ($con)
  {
         $dbhandler = mysql_select_db($database_name , $con) or die(mysql_error()); 
  }
  else
  {
      echo "not possible to connect to database";
  }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

BTW, there's no point using realpath on __FILE__, it's already the real path. Instead, just use __DIR__. Also, PHP normalizes automatically the filename when including a file, since I don't remember what version (so using include_once on foo.php and ./foo.php will include it only once).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.