passing argument to PHP script

Posted on 2001-07-15
Last Modified: 2012-08-14
myScript.php?arg is the conventional way to pass an argument to a script.  Is there a way to do it without the ? (question mark)

Any tutorials anywhere about all the different ways to treat php arguments?
Question by:anocean
  • 5
  • 4
  • 2
  • +2

Expert Comment

ID: 6284417

There 4 ways to do it:

1) In the query string as in yourScript.php?arg=1

2) By submitting a form <input type="hidden" name="arg" value="1">

3) By a cookie as in setcookie("arg","1");

4) By using a session variable
   $arg = 1;

To access the variables:

1) Use $PHP_GET_VARS["arg"] or simply $arg for method 1.

2) Use $PHP_POST_VARS["arg"] or simply $arg for method 2.

3) Use $PHP_COOKIE_VARS["arg"] or simply $arg for method 3.

4) Use $arg for session

Note that if you have variables of the same name coming from different method it is better to fully qualify it with the proper way.

Author Comment

ID: 6284421
I appreciate the trouble you went to but it didn't really answer the question, which is specifically about the format for a script URL.

Is there any way around having to use the question mark (?) immediately after .php suffix?
And is there a tutorial specifically about URL syntax for scripts?

Expert Comment

ID: 6284426
I do not think there is a way for you to do that without a ? behind. The  ? is used to differentiate the URL and the arguments passed. Without the ? there is no way to do that. :)
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 6284431
Yeah, that's kind of what I expected to hear.  Nevertheless, I'm going to leave the question open for a couple days, but will not forget to award the points if no further answers appear.

Accepted Solution

juan_tanga earned 50 total points
ID: 6284969
you can use the "/" instead of "?" in storing the args values...  well here's an example of it.



and this args are store in "PATH_INFO" environment var.
PATH_INFO = /value1/value2/value3/

to access each value, you need to split the values by "/"
$value_array = explode("/",$PATH_INFO);
$value_1 = $value_array[1];
$value_2 = $value_array[2];
$value_2 = $value_array[3];

hope this will help. :o)


Author Comment

ID: 6285058
juan tanga, your idea sounds positively spicy, but it doesn't work straight out of the box, cuz I get CGI error.
You finish this one and I'm giving you 100 point tip.

Expert Comment

ID: 6285130
may i know what error u get?

Author Comment

ID: 6285156
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

It's cuz I try URL index.php/eee/555/
and that directory, of course, doesn't exist.

Expert Comment

ID: 6285214

so u're using a cgi-module of php perhaps... I tried that one using a api-module of php and i have not test it using cgi-module version of php...

Expert Comment

ID: 6285224
try printing the value of PATH_INFO... and see if the parameter store in that environment variable..

ex. echo $PATH_INFO or echo $GLOBALS["PATH_INFO"]

Expert Comment

ID: 6285273
I think ykf200 is correct (and the answer was very complete). However, depending on your webserver, you can cheat :)

If you're using Apache, you can make it rewrite the url, I think with general regular expressions, so you can write one url and let the server transform it into one with the ? :). I assume you can do that with other servers too.

A funny example of that is the web site. Go say to (exercise caution, you may find it ofensive).


Expert Comment

ID: 6287247
you can try and read about the rewrite module for apache..

I think it can do what you waht to.

Author Comment

ID: 6300714
very imaginative.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving from Mcrypt to OpenSSL 18 45
PHP Curl to output a url 7 45
Correct syntax for subdirectories 9 18
PHP 5.6 and 7.x 4 18
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

791 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