Imaulle Posted February 24, 2010 Share Posted February 24, 2010 Hello! I'm trying to make a php script to help streamline a ton of website setups that I need to do. What I want for this function is to pull the 'username' from the url. The url will always look something like this: http://75.0.0.0/~username/folder/setup.php what is the easiest way to get just 'username' so that I can put it into a variable ? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/ Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 One way is to use $_SERVER['PHP_SELF'] which will return you "/~username/folder/setup.php", then explode it by "/", will get the result. Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017307 Share on other sites More sharing options...
Imaulle Posted February 24, 2010 Author Share Posted February 24, 2010 hmmm would this work? $username = explode('/',$_SERVER['PHP_SELF']); Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017312 Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 It will return array. $username[1] will return you actual username. Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017315 Share on other sites More sharing options...
Imaulle Posted February 24, 2010 Author Share Posted February 24, 2010 almost got it! lol with this code $geturl = explode("/",$_SERVER['PHP_SELF']); $username = $geturl[1]; it returns '~user' how do I get rid of the ~ p.s. thanks so much for helping me Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017322 Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 Use $username = substr($geturl[1], 1); Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017328 Share on other sites More sharing options...
Imaulle Posted February 24, 2010 Author Share Posted February 24, 2010 awesome! thank you dude Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017330 Share on other sites More sharing options...
Imaulle Posted February 24, 2010 Author Share Posted February 24, 2010 crap, okay new problem! lol this should be really easy but I'm only really good at c++ line 9, the $DB_NAME isn't getting printed to the file. How do I get it to print the variable name and not try to print the contents? putting it in single and double quotes didn't work $getURL = explode("/",$_SERVER['PHP_SELF']); $username = substr($getURL[1], 1); $addInfo="<?php define ('DB_USER', '".$username."_stuff'); define ('DB_PASSWORD', 'pass'); define ('DB_HOST', 'localhost'); define ('DB_NAME', '".$username."_stuff'); '$DB_NAME' = '".$username."_stuff';"; $dbinfoFile = "dbinfo.php"; $contents = file_get_contents($dbinfoFile); $contents = $addInfo.$contents; $fp = fopen($dbinfoFile,"w"); fwrite($fp,$contents); fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017364 Share on other sites More sharing options...
salathe Posted February 24, 2010 Share Posted February 24, 2010 Use \$DB_NAME Edit: see Variable parsing in double quoted strings in the PHP manual Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017367 Share on other sites More sharing options...
Imaulle Posted February 24, 2010 Author Share Posted February 24, 2010 wooo! thank you both for your excellent help Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1017369 Share on other sites More sharing options...
Imaulle Posted April 5, 2010 Author Share Posted April 5, 2010 okay new question lol. How would I get just the IP from the URL ? Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1037484 Share on other sites More sharing options...
Imaulle Posted April 6, 2010 Author Share Posted April 6, 2010 oh cool I think I got it! $getURL = $_SERVER['SERVER_ADDR']; Quote Link to comment https://forums.phpfreaks.com/topic/193190-pull-string-from-url/#findComment-1037496 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.