NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > Webmaster Tutorials
Reload this Page [TUTORIAL] Defining Paths in PHP

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search
0 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 08-18-2008, 11:56 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Apr 2005
Location: Joliet, Illinois
Posts: 1,177
RageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to behold
 


Child Abuse

[TUTORIAL] Defining Paths in PHP


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

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 Code:
<?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);
????: NamePros.com http://www.namepros.com/webmaster-tutorials/505063-tutorial-defining-paths-in-php.html
?>
Now make a directory call includes and then a file called function.php.

includes/function.php
PHP Code:
<?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 Code:
<?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");
????: NamePros.com http://www.namepros.com/showthread.php?t=505063

$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
Attached Files
File Type: zip DefiningPaths_Tutorial.zip (1.8 KB, 2 views)
RageD is offline  
Old 08-19-2008, 06:20 AM   #2 (permalink)
I'll do it
 
-Nick-'s Avatar
Join Date: Dec 2005
Location: India
Posts: 6,939
-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness
 


Member of the Month
September 2007
Adoption
Great explanation.

Can also be used somewhere where we define database settings

Thanks for writing it.
-Nick- is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 01:02 AM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger