passing variable between pages

2019-01-29 05:37发布

问题:

Is it good way to pass variable between pages using $_GET method with url:

<a href="input_obj.php?id='$id'&method=plain

and take it in file input_obj.php with such code:

$id = $_GET['id'];
$method = $_GET['method'];

OR

using session - has someone idea how?

回答1:

It depends on your needs, really, If you are passing search arguments between pages, for example, and the variables should be both persistent and be available to the end user (via bookmarking, for example), then pass them in the URL (but don't usually use quotes like you have around $id in "input_obj.php?id='$id'&method=plain)

If you are truly passing internal variables between scripts, this is better done via $_SESSION variables. Remember that end users can easily modify variables passed through URLs. Unless they are intended for use by the end user, that may be a real problem. By using $_SESSION, you insulate your script's variables from tampering by the end user when it's necessary to insulate them. (unless, of course, the variables are produced by other user input via GET/POST/COOKIE)

//page1.php
session_start();
$_SESSION['id'] = $id;

//page2.php
session_start();
echo $_SESSION['id'];


回答2:

GET variables are a much better way to go. When you start dropping variables into the session, it can have side effects like copy/pasting a URL from browser to browser or trying to bookmark can bring up different pages (which consequently is a nightmare for SEO). Also, it can have complications if you ever start clustering your servers b/c you'll need to deal with session failover.

IMHO, the best solution is to use mod_rewrite to implement path-based variables...you get pretty URLs with all the benefits of GET vars.



回答3:

GET is a reasonable way to pass variables to another page.

$_SESSION and cookies is another way, but it won't allow a user to bookmark a page.

POST is another way, but it requires form submission which would either need user intervention or javascript.



回答4:

It depends on the what the data is for, its type and its length. Usually, passing variables in the query string is fine.

Be aware that when accepting mutable parameters, you need to verify they are what you expect them to be. For example, I could change ?id=5 to ?id=hello and possibly break your application. To remedy this, we could typecast the ID to an integer:
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;



回答5:

You could also use cookies. These are sent like this:

setcookie(name, value, expire, path, domain);

you can ommit the path and domain variables. This has to be declared before the tag. The name is just the name with which you will get it. The value is what wil be returned and expire is the time at which the cookie expires (it is writen in the form of time() + $timeTillExpire where timetillexpire is a variable or constant value you set). This of course has the limitation of if the person has cookies of it will not work.

You get a cookie with:

$_COOKIE["name"];

and returns value

the way you did works fine.



回答6:

If you're just using variables in your PHP script, you don't really need to 'pass' them. You can create a variable globally and access it from another page.