-1

I am trying to include a PHP page into another PHP page.

I find it annoying that I have to change the connection info on every page when I upload it to the web host all over agin when I like to edit.

I was wondering if there was a way to include a page that way so I can keep the connection info in one file for all pages.

$username = "a";
$password = "a";
$hostname = "a.com"; 

$mysqli = new mysqli($hostname,$username , $password, 'a');

For example:

  • 1.php
  • 2.php
  • 3.php <-- this page will hold the info and be include in to pages 1 and 2
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
user1246950
  • 1,022
  • 2
  • 10
  • 19

3 Answers3

1

Use include to include other files.

1.php:

<?php

$username = "a";
$password = "a";
$hostname = "a.com"; 

$mysqli = new mysqli($hostname,$username , $password, 'a');

2.php:

<?php

include '1.php';
// Rest of your code

3.php:

<?php

include '1.php';
// Rest of your code
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
  • thx i did see this example in 3wschool but it was in to html and it didnt work for me when i tryed it, but i tryed it agin now seem to work not sure why but got what i needed so :) – user1246950 Nov 12 '14 at 20:44
1

You need to create to files auth.php and config.php
In config.php specify your variables

//config.php
    $username = "a";
    $password = "a";
    $hostname = "a.com"; 

Include your config file in auth.php

//auth.php
include('config.php');

$mysqli = new mysqli($hostname,$username , $password, 'a');

Use different config.php file on your local and web servers.:)

SergeDirect
  • 2,019
  • 15
  • 20
0

You could read a bit on AJAX .. that way you could stay on the same page sending info asynchronously

Onilol
  • 1,315
  • 16
  • 41