note 112694 added to language.constants.predefined
| From: | wyattstorch42 at outlook dot com | Date: | Sat, 13 Jul 2013 18:19:04 +0000 |
| Subject: | note 112694 added to language.constants.predefined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195606@lists.php.net to get a copy of this message | ||
<?php
/*
* I wrote this because I was including a file with classes in it. Let's say that
* I have a contact page at mysite.com/contact/index.php and a Form class at
* mysite.com/classes/Form.php. So in index.php, I have this statement:
* require '../classes/Form.php';
* The Form class includes a method to generate the HTML markup for a number of
* form elements, including a CAPTCHA image and associated text field. To do so,
* it must generate an <img /> element and give it a src of Form.php?captcha.
* But I wanted it to automatically generate a src attribute without index.php
* giving it a relative path. This script comes in handy by automatically
* locating the directory that contains the included file (Form.php) and converting
* it from an absolute path to a relative path that could be used for an img src,
* an a href, a link href, etc.
*/
function relativeURL () {
$dir = str_replace('\\', '/', __DIR__);
// Resolves inconsistency with PATH_SEPARATOR on Windows vs. Linux
// Use dirname(__FILE__) in place of __DIR__ for older PHP versions
return substr($dir, strlen($_SERVER['DOCUMENT_ROOT']));
// Clip off the part of the path outside of the document root
}
/*
*contact/index.php
*/
require '../classes/Form.php';
new Form()->drawCaptchaField();
// Writes: <img src="/classes/Form.php?captcha" />
/*
* classes/Form.php
*/
if (isset($_GET['captcha'])) {
// generate/return CAPTCHA image
}
class Form {
// ...
public function drawCaptchaField () {
echo '<img src="'.relativeURL().'?captcha" />';
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 70.190.171.208
----
Manual Page -- http://php.net/manual/en/language.constants.predefined.php
Edit -- https://master.php.net/note/edit/112694
Del: integrated -- https://master.php.net/note/delete/112694/integrated
Del: useless -- https://master.php.net/note/delete/112694/useless
Del: bad code -- https://master.php.net/note/delete/112694/bad+code
Del: spam -- https://master.php.net/note/delete/112694/spam
Del: non-english -- https://master.php.net/note/delete/112694/non-english
Del: in docs -- https://master.php.net/note/delete/112694/in+docs
Del: other reasons-- https://master.php.net/note/delete/112694
Reject -- https://master.php.net/note/reject/112694
Search -- https://master.php.net/manage/user-notes.php