[TUTORIAL] Defining Paths in PHP

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch
As many of you can probably see I haven't been around the forum in around a month or so. Been kind of busy upon other things and have been getting a lot of questions about doing different things in PHP. So this may be the first in possibly a series of tutorials to get all questions answered :p

In this tutorial I'll explain "define" pretty much and how to get it to define something like "ROOT_PATH". (not being so much a variable)

Unless you feel like defining everything in every file, I suggest the best way to define is make a file called something like "init.php". That's what I'll be using for the example.

init.php
PHP:
<?php
/**
 * init.php for "Defining Paths in PHP" Tutorial by RageD
 * (C) 2008 RageD
 *
 */

/**
 * This first one is simply for security. Accessing functions
 * without proper access is bad :(
 *
 */
define("SITE_SCRIPT", true);

/**
 * Here we'll define the root path. Make sure that this file
 * is in the ROOT directory! :)
 *
 */
define("ROOT_PATH", dirname(__FILE__));

/**
 * And finally.. Our includes. In this example it will be the
 * "includes" directory.
 *
 */
define("INC_PATH", ROOT_PATH . '/includes');

/**
 * Final step... Make sure that this worked properly
 * :)
 *
 */
define("INIT_DONE", true);
?>

Now make a directory call includes and then a file called function.php.

includes/function.php
PHP:
<?php
/**
 * includes/function.php for "Defining Paths in PHP" Tutorial by RageD
 * (C) 2008 RageD
 *
 */
// This is where that security definition comes into play :)
if(!defined("SITE_SCRIPT"))
{
     print("Unauthorized Access!");
     exit;
}

// Next, just a simple class and function example.
class Test
{
	/**
	 * Constructor :)
	 *
	 */
	function __construct()
	{
		$this->text = "Hello World!<br />This script actually does work! :D";
	}

	/**
	 * Basically just print $this->text
	 *
	 */
	function hello()
	{
		echo($this->text);
		exit;
	}
}
?>

Now finally just put this in an index.php or something so you can view it all done right :)

index.php
PHP:
<?php
/**
 * includes/function.php for "Defining Paths in PHP" Tutorial by RageD
 * (C) 2008 RageD
 *
 */

// Place this file in the ROOT directory with init.php
require_once("init.php");

// Check if it worked properly or not
if(!defined("INIT_DONE"))
{
	echo("Doh! Houston, we have a problem. We did not initiate correctly!");
	exit;
}

// Get our function :D
require_once(INC_PATH . "/function.php");

$test = new Test; // Define our class so we can perform the function.

$test->hello(); // Run the function!
?>

I've even attached a copy of the script for anyone interested.

Any questions, let me know :)

-RageD
 

Attachments

  • DefiningPaths_Tutorial.zip
    1.8 KB · Views: 130
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Great explanation. :)

Can also be used somewhere where we define database settings :)

Thanks for writing it.
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back