?
Solved

Retrieve part of a Typed URL into a variable

Posted on 2013-02-01
5
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

Accepted Solution

by:
Ray Paseur earned 1000 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 1000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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