Jump to content

substr_replace, have just a couple questions about it.


Recommended Posts

I am writing an install script and a bump i have run into is when i am using the substr_replace to trim up a url that i get from people that are using the install script.

 

I know that the page name is index.php and what i would like to do is delete the page/file extension (install.php) from the url i get back.

 

The problem that i am having is when people navigate to the page and lets say they enter in the address bar:

http://example.com

This script will still see that they have navigated to index.php but thats not that url that they have typed in so it will then delete 9 chars (index.php) from the right leaving:

http://ex

 

If they navigate using:

http://example.com/index.php

it works perfectly.

 

Here is my code:

 

<?php
function curPageURL() {
$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
$port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
$url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
return $url;
}
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$str = curPageName();

$originalUrl = curPageURL();
$cleanedUrl = substr_replace($originalUrl, "", 0, 0);
$cleanedUrl2 = substr_replace($cleanedUrl, "", -strlen($str), strlen($str));

$web_url = $cleanedUrl2;
?>

 

If anybody could help me figure out how to get this to work i would greatly appreciate it.

 

Also if there is a way to check to see what the user has typed into the address bar i could figure out a work around.

Well, you could do something like:

 

if(strpos($_SERVER['REQUEST_URI'], "index.php") !== false){
//index.php was found in the url, perform your substr_replace()
}else{
//index.php was not found
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.