Skip to main content


Showing posts from July, 2016

PHP - database configuration phpfile

You can try to put your database credentials in separate file with proper UNIX permissions set, for example 644, and then include this file on top of your script. The configuration.php file will look like: <?php define (DB_USER,"mysql_user"); define (DB_PASSWORD,"mysql_password"); define (DB_DATABASE,"database_name"); define (DB_HOST,"localhost");?> Your original script will look something like this: require("configuration.php");publicclassDatabaseConnect{function __construct(){ mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)ordie('Could not connect to MySQL server.'); mysql_select_db(DB_DATABASE);}}

Creating a PHP header/footer

source: Besides just using include() or include_once() to include the header and footer, one thing I have found useful is being able to have a custom page title or custom head tags to be included for each page, yet still have the header in a partial include. I usually accomplish this as follows: In the site pages: <?php $PageTitle="New Page Title";function customPageHeader(){?><!--Arbitrary HTML Tags--><?php } include_once('header.php');//body contents go here include_once('footer.php');?> And, in the header.php file: <!doctype html><html><head><metahttp-equiv="content-type"content="text/html; charset=UTF-8"><title><?= isset($PageTitle)? $PageTitle :"Default Title"?></title><!-- Additional tags here --><?php if(function_exists('customPageHeader'

PHP MVC - LoginController

Source: Request example Put something like this in your index.php: <?php // Holds data like $baseUrl etc. include 'config.php'; $requestUrl ='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $requestString = substr($requestUrl, strlen($baseUrl)); $urlParams = explode('/', $requestString); $controllerName = ucfirst(array_shift($urlParams)).'Controller'; $actionName = strtolower(array_shift($urlParams)).'Action';// Here you should probably gather the rest as params// Call the action $controller =new $controllerName; $controller->$actionName(); Really basic, but you get the idea... (I also didn't take care of loading the controller class, but I guess that can be done either via autoloading or you know how to do it.) Simple controller example (controllers/login.php):