Solved

Retrieve part of a Typed URL into a variable

Posted on 2013-02-01
5
272 Views
Last Modified: 2013-02-01
Hi folks, I'm in need of help with the following situation:

I'm making a php page that retrieves the number typed after my domain from the user and uses it to verify it against a certain variable.

So what I would need would be the number typed in something like the following:

www.mydomain.com/123456

Is there a way to retrieve the typed number? I want the number and the page or folder doesn't exist since it is a variable...

Any help would be greatly appreciated.

Cheers!
0
Comment
Question by:rdoc76
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38843459
$mydomain = "www.mydomain.com/123456";
$pattern = '/\D\d+/';
preg_match($pattern, $mydomain, $matches);
$number = substr($matches[0], 1);
echo $number;

Open in new window

or :
$mydomain = "www.mydomain.com/123456";
$number = explode("/", $mydomain);
echo $number[1];

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38843517
This would imply that your server has a directory named 123456 and your script would then be the index.html inside that directory:

www.mydomain.com/123456

If you did it this way, your script could find 123456:

www.mydomain.com?123456

Example: http://www.laprbass.com/RAY_bounce_get.php?123456
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38843525
I think this would be more complicated than retrieving the value from the string.
Your address may be like www.mydomain.com/123/456/abc/def
how do you want - and what variables - to get them ?

Here's the little function

        function getMyURL(){
            $host = $_SERVER['HTTP_HOST'];
            $self = $_SERVER['PHP_SELF'];
            $par = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
            $url = !empty($par) ? "http://$host$self?$par" : "http://$host$self";
            return $url;
        } 

Open in new window


Then you can explode it with "/" and get all variables in an array.
0
 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 250 total points
ID: 38843536
Everything after the host name (and port number if specified) is called the URI.  In PHP you can retrieve the URI using:

 $_SERVER['REQUEST_URI']

In your example  $_SERVER['REQUEST_URI'], would return "/123456".
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38843656
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question