Page 1 of 1

should I make a database class? init.inc.pnp file

Posted: Thu Aug 18, 2011 11:26 pm
by wizzuriz
If I like to call $database on a other page will it do just to make it a variable or should I make it a class?

I´m all new on this and is ready some php & mysql books. but right now I´m working on the user register system.

If I make a variable on one page and include it on a other page how can I then call that variable?
<?php

session_start();

$exceptions = array('register','login','activation');

$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

$databse = mysql_connect('localhost', 'root', 'My_pass');
mysql_select_db('user_system', $databse);


$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

if (isset($_COOKIE['username'], $_COOKIE['password']) && isset($_SESSION['username']) === false){
    if (vaild_credentials($_COOKIE['username'], $_COOKIE['password'])){
        $_SESSION['username'] = htmlentities($_COOKIE['username']);
        
        setcookie('username', $_COOKIE['username'], time() + 604800);
        setcookie('password', $_COOKIE['password'], time() + 604800);
    }
}


if (in_array($page, $exceptions) === false){
    if(isset($_SESSION['username']) === false){
        header('location: login.php');
        die();
    }
}


?>

This is my init.inc.php file

best regards
Wizzuriz

Re: should I make a database class? init.inc.pnp file

Posted: Fri Aug 19, 2011 4:28 pm
by Dominion
If you're after a connection identifier then it's -
$data = mysql_connect('','','');
$database = mysql_select_db('',$data);
Then use $database. As for calling them in another file, if the file is included so are all set values e.g. if the above is connect.php
include 'connect.php';
// can call other values
echo $data;
Would have the values of both $data, and $database set and ready to call. of course no php tags etc but you get the point.

Re: should I make a database class? init.inc.pnp file

Posted: Fri Aug 19, 2011 9:24 pm
by Kamal
No, you shouldn't make one. You should use this one: https://github.com/LearnWebDev/PHP-Database-Class