How many ways to pass values from one php file to another?
So far, I know following ways to pass values from one php file to another:
Could you add more methods and examples if possible?
You can pass data through an URL, it should be in the form of key/value pairs, but you shouldn't use it to pass too much data because an URL has a limit. You also should not pass sensitive information.
A key/value pair is something like this:
If you have more then one pair, you need to separate them using the & char. Here is an example:
The string after ? is called the Query String. With PHP you can easily access those key/value pairs using the super-global $_GET. So, in myScript.php you do:
$a = $_GET['color1'];
$b = $_GET['color2'];
Now, if you are going to create a dynamic query string, you should use urlencode() at least, so any special characters will be translated to maintain a proper URL format.
2. include() or require() the second file, and it will inherit all global variables.
3. Use cURL to send it via post data.
$foo = 'bar';
include('file2.php'); // file2.php will have access to $foo in the global scope
4. Store the data of interest in a session variable or in a cookie, and access that same session variable or cookie in the 2nd file (both must run in same domain)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
4. In the session
5. Common file -- maybe a user data/xml file.
$_SESSION['key'] = $val;
$val = $_SESSION['key'];
6. By Cookie
$xml = file_get_contents($somefile);
(don't do it this way)
7. Through another commonly accessible process
- Message Queue
You should only use GET when it doesn't MODIFY the contents. So for example, you shouldn't use get to delete something (though people often do), you should use get for things such as pagination or viewing a forum post.
Originally Posted by dmaarc123
POST should be used for deleting, editing, that kind of request.
Other ways you can send data between pages include (though no way am i giving you examples, google it):
- Databases, MySQL etc
- Sessions (file or database powered)
- Memcache (storing data in RAM)
- GET query of a URL
- Cookies - stored in files on web browsers
- Hidden form fields
cURL is not a way of transfering data from one page to another in the context you are putting it in.
And for people to laugh at, my original explanation of cURL for this post was lmao - just had a 7 hour appointment building a website for a client, give me a break
cURL is not a way of transfering data
from one page to another - instead its the technology that retrieves or sends data
to other pages, it doesn't actually store anything... if that makes sense :/
Last edited by VBAssassin; 05-31-2012 at 11:55 AM.
And if you want to get elaborate and silly, you could send data to an external commonly accessible system. For instance, you could email the common data from one page and read the messages from another. Or post it to google docs or some other online storage from one and read from another. Or a forum. Or a chatroom. Etc.
That depends entirely on how you interpret the question!
Originally Posted by VBAssassin
And, I find it hilarious an ironic that you say, "cURL is not a way of transfering data from one page to another," and then go on to immediately say, "instead its the technology that retrieves or sends data to other pages." Well, which is it!
Frankly, I think the question is vague. It doesn't indicate whether one page should be passing data directly to another, or whether the data to be passed is from user initiated page load to user initiated page load.
Originally Posted by VBAssassin
You can always print the variables, then use a scanner. Fax is pretty reliable too
Originally Posted by kbduvall
Or, from the first page, send the visitor an invoice (paper invoice via snail mail, of course) on which the line items represent the data structures in JSON. Include instructions to navigate to the 2nd page and enter the line item data.
We can get values from other file by using $_GET, $_POST and $_SESSION methods.
Users Browsing this Thread
There are currently 2 users browsing this thread. (0 members and 2 guests)