Several times it becomes necessary to find the url of the current page in php. It is not a very big deal to find it, but for a newbie it may take quite some time. So here is a function to do that. The url is stored in a static variable so that it doesn't have to be generated everytime the function is called.
<?php
function getCurrentPageUrl (){
static $pageURL = '' ;
if(empty( $pageURL )){
$pageURL = 'http' ;
if(isset( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] == 'on' ) $pageURL .= 's' ;
$pageURL .= '://' ;
if( $_SERVER [ 'SERVER_PORT' ] != '80' ) $pageURL .= $_SERVER [ 'SERVER_NAME' ]. ':' . $_SERVER [ 'SERVER_PORT' ]. $_SERVER [ 'REQUEST_URI' ];
else $pageURL .= $_SERVER [ 'SERVER_NAME' ]. $_SERVER [ 'REQUEST_URI' ];
}
return $pageURL ;
}
?>
// In this example we are storing the url of the current page as a hidden form field so that we can return to the previous page after completing an action, say login
example usage
<html>
<head>
<title>Current Page URL</title>
</head>
<body>
<form method="post" action="/users/login.php">
Username : <input type="text" name="username" value=""/><br/>
Password : <input type="password" name="password" value=""/>
<input type="submit" value="Login"/>
<input type="hidden" name="action" value="login"/>
<input type="hidden" name="return_url" value="<?php echo getCurrentPageUrl (); ?> "/>
</form>
</body>
</html>
Simple image counter Categories : PHP , Graphics , Filesystem , Beginner Guides Making a simple Hit-Log using PHP and MySql Categories : PHP , Log Files , Beginner Guides , Databases , MySQL Using PHP to show text in random colors Categories : PHP , Beginner Guides , Strings Newbie Notes #10 - Generating drop downs Categories : PHP , MySQL , HTML , Beginner Guides , Databases Temperature Conversion Categories : PHP , Math. , Beginner Guides Ensure that a specific value lies within a specific range. Categories : PHP , Beginner Guides , Data Validation filter untrusted GET and POST variables and create trusted variable of same name Categories : PHP , Global Variables , Security Find if a year is leap. Categories : PHP , Date Time , Beginner Guides , Data Validation Count Number Of weeks in Month Categories : PHP , Date Time , Beginner Guides How can I know about what Operating System is running on server from
PHP? Categories : PHP , Global Variables Validating a URL with preg_match Categories : PHP , Regexps , Beginner Guides , Data Validation Sending mail to a mailing list and showing progress Categories : PHP , Mail , Beginner Guides How to display any array in several rows and columns of a table. Not just
in one column or in alternate rows. This example shows a nice color table
generated with PHP, but can be used with any array values(e.g. Database) Categories : Arrays , PHP , Miscellaneous , Beginner Guides , Graphics Kewl Date Example Categories : PHP , HTML and PHP , Date Time , CSS , Beginner Guides Newbie Notes #9 - Hyperlinking a post Categories : PHP , Java Script , HTML and PHP , Beginner Guides