How to Pass Parameters into a PHP Script Through a Webpage

How do I pass parameters into a PHP script through a webpage?

Presumably you're passing the arguments in on the command line as follows:

php /path/to/wwwpublic/path/to/script.php arg1 arg2

... and then accessing them in the script thusly:

<?php
// $argv[0] is '/path/to/wwwpublic/path/to/script.php'
$argument1 = $argv[1];
$argument2 = $argv[2];
?>

What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:

Go to http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2

... and access:

<?php
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
?>

If you want the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

as pointed out by Cthulhu in the comments, the most direct way to test which environment you're executing in is to use the PHP_SAPI constant. I've updated the code accordingly:

<?php
if (PHP_SAPI === 'cli') {
$argument1 = $argv[1];
$argument2 = $argv[2];
}
else {
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
}
?>

Passing parameter to a PHP script from javaScript

The simplest method would be to add a parameter to the URL that you're fetching from:

fetch('getData.php?id=1').then(...)

In your getData.php script check for the parameter in the $_GET[] array:

if (!isset($_GET['id']) {
// Do something by default
} else {
switch($_GET['id']) {
case '1':
// do stuff
break;
case '2':
// do different stuff
break;
}
}

PHP passing parameters via URL

You should use .htaccess

Here's an example:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [NC,L]

This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.

Example:

mysite.com/20/this_is_a_new_article/

Would actually mean

mysite.com?var1=20&var2=this_is_a_new_article

Of course, in your index.php file you can simply take the values using

$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

Cheers!

How do I pass parameters to a php script in a bat file

CLI parameters work differently. You can run it as

C:\xampp\php\php.exe -f "C:\xampp\htdocs\path\here\script.php" -- email=true

notice the first --, it separates php.exe parameters from parameters to your script.

But that is not enough, because parameters are now stored in $argv array. So you could check it as $sendEmail = in_array('email=true', $argv);.

Alternatively (you need more options), you can use things like getopt.

If you need it to be in $_GET check out also this SO answer.

How to pass a PHP variable using the URL

In your link.php your echo statement must be like this:

echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';

Then in your pass.php you cannot use $a because it was not initialized with your intended string value.

You can directly compare it to a string like this:

if($_GET['link'] == 'Link1')

Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.

PHP Pass variable to next page

HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.

Session:

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

Cookie:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.

GET and POST

You can add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable.

Another way is to include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>

And then on page two:

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.

Passing variables and data through a regular web page link?

If you have a plain link you can pass them as query parameters on the link (which is a GET operation)

<a href="/myapp/somecontroller.php?id=17&name=fred">Save</a>

If you have a plain HTML form with no javascript to handle the button press you can put things in hidden fields

<form name="theform" method="post" action="/myapp/somecontroller.php">
<input type="hidden" name="id" value="17">
<input type="hidden" name="name" value="fred">
...
</form>

This doesn't require any javascript.

Passing parameters from one PHP to WEB SITE

You already output content to use header().
I would recommend:

$sRemotePage = file_get_contents('https://localhost/test=123');

More here: http://php.net/manual/en/function.file-get-contents.php

Call a php function passing argument on a link in the same page and display the content in div

So you pass $_GET variable via url like that:

<!-- lcoalhost/myPage.php -->
<a href='myPage.php?var1=123&var2=abc+bcd'>Link here</a>

And in PHP:

$var1 = $_GET['var1']; // => (string) '123'
$var2 = $_GET['var2']; // => (string) 'abc bcd'


Related Topics



Leave a reply



Submit