0

Started learning php this week and had many doubts, but this is one of them which i couldn't find solution(Maybe i didn't know the right keyword to search for).

Is it possible to read variables in URL as a function inside a php file, for example :

  http://mywebsite.com/demo_app/phone_api/login/harsha/harshapass

Here demo_app is the folder, phone_api is the php file, and i want to invoke the function login, where harsha and harshapass is the paramaters to that function.

1
  • 1
    Do you intend on parsing this information or just having it magically do it all by itself? Commented Jun 25, 2015 at 9:59

2 Answers 2

1

You'll need 2 things for this.

The first is mod_rewrite, this is an apache module. With this module you can rewrite the URL like you want, so that /demo_app/phone_api/ will redirect to your php file.

I advice that you read a tutorial somewhere about this, for example here.

Second thing is that you need to parse the URL.

With $_SERVER['REQUEST_URI'] you get the URL. With explode and/or preg_match you can parse this string into the parts you want (function, parameters, whatever).

If you have a more specific question about this, you can ask this here (in a new topic).

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

Did you consider sending the values as $_GET variables and retrieving them in the page like so:

  http://mywebsite.com/demo_app/phone_api?login=login&harsha=harsha&harshapass=harshapass

so you can get the values in the phone_api.php page like:

$login = $_GET['login'];
$harsha = $_GET['harsha'];
$harshapass = $_GET['harshapass'];
$login($harsha, $harshapass){
  //your function code.
}

Maybe it'll be easier this way.

1 Comment

Yes i'm aware of that method and i know to use that way, But i'm looking for different method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.