mattyf Posted June 21, 2007 Share Posted June 21, 2007 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! Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 21, 2007 Share Posted June 21, 2007 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']); ?> Quote Link to comment Share on other sites More sharing options...
mattyf Posted June 21, 2007 Author Share Posted June 21, 2007 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? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 21, 2007 Share Posted June 21, 2007 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? Quote Link to comment Share on other sites More sharing options...
mattyf Posted June 21, 2007 Author Share Posted June 21, 2007 Sorry, yes I meant to say PHP version 4.3.9. PHP_SELF could provide a solution with a little more assistance <?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 . Quote Link to comment Share on other sites More sharing options...
mattyf Posted June 22, 2007 Author Share Posted June 22, 2007 Fixed it! <?php $_GET['mods']=preg_replace('#^/directory/([a-z]*)\.php/(.*)\.(php|html)$#', '\\1.\\2',$_SERVER['PATH_INFO']); ?> Thanks for your guidance Wildbug! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.