note 112693 added to reserved.variables.server

From: Date: Sat, 13 Jul 2013 18:18:07 +0000
Subject: note 112693 added to reserved.variables.server
Groups: php.notes 
Request: Send a blank email to php-notes+get-195605@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/reserved.variables.server.php Edit -- https://master.php.net/note/edit/112693 Del: integrated -- https://master.php.net/note/delete/112693/integrated Del: useless -- https://master.php.net/note/delete/112693/useless Del: bad code -- https://master.php.net/note/delete/112693/bad+code Del: spam -- https://master.php.net/note/delete/112693/spam Del: non-english -- https://master.php.net/note/delete/112693/non-english Del: in docs -- https://master.php.net/note/delete/112693/in+docs Del: other reasons-- https://master.php.net/note/delete/112693 Reject -- https://master.php.net/note/reject/112693 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195605) next »