Php pass parameters to url

Passing a PHP Variable in the URL

I currently have a login form and I want to pass the username across the URL. It is stored in a session variable. I have this on the page after the login but it doesn’t seem to be working.

$myusername = $_POST['myusername']; 

The login page is a php page that connects to another php that checks the login details from mySQL database. The main part from that is this (on the checklogin php page)

// username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; 

6 Answers 6

Edit, looking at your edits, I’d say the first section about the redirect looks like what you need

It is stored in a session variable.

Are you formatting a URL string from a php session and then trying to transmit get params somewhere? If so, did you try using the session API to get the username and then doing a php redirect?

 session_start(); $username = $_SESSION['myusername']; // redirect header("http://your/sites/dir/page.php?myusername=$username"); 

But it looks like you’re relying on the deprecated session_register and register_globals, based on this comment:

I am wanting to pass this throughout the application, I do have it saved as a session I think as this: session_register(«myusername»);

That’s probably not using because register_globals has been turned off. You should leave it off. You want to avoid session_register and instead use the $_SESSION array.

Here’s how you pull a «myusername» from a URL GET query

To extract the username from a URL you want to use $_GET, ie

 $myusername = $_GET['myusername']; 

The part of the URL you’re looking at is known as the «query string». The parameters formatted

are known as «GET» parameters. $_POST parameters are not transmitted in the URL itself. You can read more about HTTP GET vs POST, here.

To save the username in the session do

 session_start(); // be sure to VALIDATE your inputs $_SESSION['myusername'] = $_GET['myusername']; // replace with post if appropriate 

Источник

How can I pass POST parameters in a URL?

Basically, I think that I can’t, but I would be very happy to be proven wrong. I am generating an HTML menu dynamically in PHP, adding one item for each current user, so that I get something like > , but I have a preference for POST over GET. Is there a way to pass the information as a POST parameter, rather than GET from a clickable HREF link? I am not allowed to use JavaScript. It looks like Rob is on to something with «You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST»

Читайте также:  Print value in string python

You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST.

In addition to what Ben said, you can also let the link be a dummy and have it execute a javascript that submits a hidden form. As forms can be a bit picky on how they are nested, this might make it easier.

7 Answers 7

You could use a form styled as a link. No JavaScript is required:

This works. Just to add if he wanted the link to be consistent with his other links, he might add it in his stylesheet as a:link, button.class <> , and a:hover, button.class:hover <> , etc.

Parameters in the URL are GET parameters, a request body, if present, is POST data. So your basic premise is by definition not achievable.

You should choose whether to use POST or GET based on the action. Any destructive action, i.e. something that permanently changes the state of the server (deleting, adding, editing) should always be invoked by POST requests. Any pure «information retrieval» should be accessible via an unchanging URL (i.e. GET requests).

To make a POST request, you need to create a . You could use Javascript to create a POST request instead, but I wouldn’t recommend using Javascript for something so basic. If you want your submit button to look like a link, I’d suggest you create a normal form with a normal submit button, then use CSS to restyle the button and/or use Javascript to replace the button with a link that submits the form using Javascript (depending on what reproduces the desired behavior better). That’d be a good example of progressive enhancement.

Источник

Читайте также:  Цвета в html для мта

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

I am calling a PHP script whenever a webpage loads. However, there is a parameter that the PHP script needs to run (which I normally pass through the command line when I am testing the script). How can I pass this argument every time the script is run when the page loads?

Okay, thank you guys. I got the answers below: use $GET and pass it as a parameter value in the url itself.

2 Answers 2

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:

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

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:

I would use empty($_GET) . $_GET is a predefined value, so I’m pretty sure it is always set, but empty if there are no GET parameters set. empty() returns false for empty strings and arrays.

Without actually checking, I do believe that $_GET is in fact not set if the script is called in a command line context. empty() would cause it to look for command line arguments if the script is accessed from the web without a query string, thus throwing more notices.

$argv[0] isn’t actually arg1 in your 1st example, it would be «/path/to/wwwpublic/path/to/script.php». (php.net/manual/en/reserved.variables.argv.php)

@emanueledelgrande I’m not entirely sure I follow what you’re asking, but I’ll try. .htaccess is an Apache file — Apache processes incoming requests with the directives in .htaccess and then invokes PHP to build the response to the request. So, if you’re in the command line, .htaccess will be completely ignored. If you’re accessing a script through the web, and you want to add new querystring arguments after receiving the request, that’s typically done with mod_rewrite . If that doesn’t sufficiently answer your question, I suggest you post a brand new question.

Читайте также:  Строки

Источник

How I can pass multiple parameters to URL in php

$_GET[‘part_id’] this var from another URL and I want to pass it again and $_GET[‘quantity’] quantity from form.

3 Answers 3

You must use urlencode function for your parameters, and don’t use the double quotes:

Even though, I suggest you to avoid long urls and use POST with hidden fields.

thank you for your help but I still have the same problem ( ( ! ) Notice: Undefined index: quantity in C:\wamp\www)

@black This notice means, that $_GET[‘quantity’] element is undefined. It has nothing to do with the link. To avoid the notice, use the yannis hristofakis suggested.

You get this notice Undefined index: quantity cause you are trying to access an undefined variable. If your page url is something like page_name.php?quantity=5 then $_GET[‘quantity] is set, otherwise it isn’t.

You should check if the $_GET variable exist. If so, use @user4035 answer to print the url.

There’s a few errors that I can see.

You can’t use $_GET[‘quantity’] if the user is entering it in without reloading the page (Remember, PHP is not client-side). Thus, I suggest using JavaScript for that.

function buildUrl(a) < var qty = document.getElementById("qty").value; a.href = "./shopping-cart.php?part_id="++"&quantity text" name="quantity"> //onclick attribute triggers the JavaScript 

Although you answer is very good,I think you shouldn’t point him to a client side solution. He is missing fundamentally things such as POST-GET request,and an answer such as your will only make things more difficult for him. Consider that he might don’t even know javascript . If you think my vote is unjustified you can edit you answer and I will undo.

@yannishristofakis that’s a fair point, I’ll leave it up though in case someone would like to try it this way.

Источник

Оцените статью