www.webdeveloper.com
Results 1 to 12 of 12

Thread: How many ways to pass values from one php file to another?

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Angry 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:

    Code:
    1. parameter
       i.e. receiver.php?key=10
    2. unknowing 
       ...
    Could you add more methods and examples if possible?

    Thanks

    .

  2. #2
    Join Date
    May 2012
    Location
    United State
    Posts
    79
    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:

    key=value

    If you have more then one pair, you need to separate them using the & char. Here is an example:

    myScript.php?color1=blue&color2=red

    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.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    2. include() or require() the second file, and it will inherit all global variables.
    PHP Code:
    <?php
    $foo 
    'bar';
    include(
    'file2.php'); // file2.php will have access to $foo in the global scope
    3. Use cURL to send it via post data.

    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

    eBookworm.us

  4. #4
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    4. In the session
    PHP Code:
    $_SESSION['key'] = $val;
    $val $_SESSION['key']; 
    5. Common file -- maybe a user data/xml file.
    PHP Code:
    file_put_contents($somefile$xml)
    $xml file_get_contents($somefile); 
    6. By Cookie
    (don't do it this way)

    7. Through another commonly accessible process
    1. MySQL
    2. Message Queue
    3. Memcache
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  5. #5
    Join Date
    May 2012
    Posts
    59
    Quote Originally Posted by dmaarc123 View Post
    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.
    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.

    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
    • etc


    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 :/
    Kind regards,
    Scott
    Last edited by VBAssassin; 05-31-2012 at 11:55 AM.

  6. #6
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    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.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  7. #7
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by VBAssassin View Post
    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 :/
    That depends entirely on how you interpret the question!

    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!
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  8. #8
    Join Date
    May 2012
    Posts
    59
    Quote Originally Posted by svidgen View Post
    That depends entirely on how you interpret the question!

    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!
    Haha, yeah, i did! technically it does transfer data... the same way TCP/IP does, or even fopen using streams in PHP. But it's not what he's asking in the question and i confused myself trying to explain how it wasn't what he was asking for lmao

    Make sense now? Or at least i should be saying, does my excuse for contradicting myself make sense?

    Kind regards,
    Scott

  9. #9
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by VBAssassin View Post
    Haha, yeah, i did! technically it does transfer data... the same way TCP/IP does, or even fopen using streams in PHP. But it's not what he's asking in the question and i confused myself trying to explain how it wasn't what he was asking for lmao

    Make sense now? Or at least i should be saying, does my excuse for contradicting myself make sense?

    Kind regards,
    Scott
    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.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  10. #10
    Join Date
    May 2012
    Posts
    42
    You can always print the variables, then use a scanner. Fax is pretty reliable too
    Keith

    Freelance PHP Web Developer
    http://www.duvalltech.com/

  11. #11
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by kbduvall View Post
    You can always print the variables, then use a scanner. Fax is pretty reliable too
    Ah, touche!

    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.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  12. #12
    Join Date
    Apr 2012
    Posts
    16
    We can get values from other file by using $_GET[], $_POST[] and $_SESSION[] methods.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles