Solved

Retrieve part of a Typed URL into a variable

Posted on 2013-02-01
5
267 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now