Jump to content

[SOLVED] Help needed with ereg_replace


mattyf

Recommended Posts

Hello

 

I've inherited a site that was running on IIS and I have no previous experience with ereg_replace. The code below (companies.php) works on the IIS server but returns a 404 on my Linux VPS.

 

From what I can ascertain, the second line of code should return companies.login from the following address:

 

http://www.domain.com/folder/companies.php/login.html

 

<?php
$_SERVER['PATH_INFO']=ereg_replace('/$','/index.html',$_SERVER['PATH_INFO']);

$_GET['mods']=ereg_replace('\.php$','',substr($_SERVER['PHP_SELF'],15)).ereg_replace('\.(php|html)$','',$_SERVER['PATH_INFO']);

$_GET['ext']=ereg_replace('.*\.','',$_SERVER['PATH_INFO']);

require('index.php');
?>

 

If I hard code

$_GET['mods'] ="companies.login";

it works for that one page, but then obviously not for others.

 

I'd greatly appreciate any pointers.

 

Thanks!

Link to comment
Share on other sites

I'm not a POSIX regex expert, but I think this is what that means:

 

<?php

// Replace a trailing slash in $_SERVER['PATH_INFO'] with /index.html
$_SERVER['PATH_INFO']=ereg_replace('/$','/index.html',$_SERVER['PATH_INFO']);

/* Set $_GET['mods'] equal to the first 15 characters of $_SERVER['PHP_SELF'],
minus a possible ".php" on the end, plus $_SERVER['PATH_INFO'] less a trailing
".php" or ".html" */
$_GET['mods']=ereg_replace('\.php$','',substr($_SERVER['PHP_SELF'],15))
. ereg_replace('\.(php|html)$','',$_SERVER['PATH_INFO']);

// Set $_GET['ext'] equal to anything after the last period (i.e., the ".php" or ".html") in $_SERVER['PATH_INFO']
$_GET['ext']=ereg_replace('.*\.','',$_SERVER['PATH_INFO']);
?>

Link to comment
Share on other sites

Thank you for taking the time to reply, that's really useful.

 

It looks like it may be an issue with the use of

$_SERVER['PATH_INFO']

 

I made a page

<?php
print 'PATH_INFO: '. $_SERVER['PATH_INFO']. "<br>\n";
print 'ORIG_PATH_INFO: '. $_SERVER['ORIG_PATH_INFO']. "<br>\n";
?>

and set acceptpathinfo=On

 

but I just get:

 

PATH_INFO:

ORIG_PATH_INFO:

 

Apache version is 4.3.9 - maybe path_info isn't supported?

Link to comment
Share on other sites

It might be.  I didn't get PATH_INFO either (I did get ORIG_PATH_INFO on phpinfo()), but I figured it might be a configuration or version issue on my end.  If you don't have it either, that's probably your problem, and you'll need to find a better variable with the same/comparable value to extract the data from.  Do you know what data the original script needed from those vars?

 

You mean PHP version 4.3.9?

Link to comment
Share on other sites

Sorry, yes I meant to say PHP version 4.3.9.

 

PHP_SELF could provide a solution with a little more assistance  ;D

 

<?php
$_SERVER['PATH_INFO']=ereg_replace('/$','/index.html',$_SERVER['PHP_SELF']);

$_GET['mods']=ereg_replace('\.php$','',substr($_SERVER['PHP_SELF'],0)).ereg_replace('\.(php|html)$','',$_SERVER['PHP_SELF']);

print 'PATH_INFO: '. $_SERVER['PATH_INFO']. "<br>\n";
print 'mods: '. $_GET['mods']. "<br>\n";
?>

 

Outputs:

 

PATH_INFO: /directory/companies.php/login.html

mods: /directory/companies.php/login.html/directory/companies.php/login

 

But in this example I need mods to = companies.login

 

I've tried a number of combinations, and can achieve companies/login but can't quite work out how to replace the / with a .

Link to comment
Share on other sites

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.