treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How do I send a PHP variable to another file.

  • How would I send a php variable to another php file and then redirect the user to a totaly different file?
  • "mattvot" said:
    How would I send a php variable to another php file?


    You don't really send variables in php. If you include another php file, the included file will have access to all global variables.

    If you want to keep the value of some variable between page requests then you will need to store the value somewhere. The easiest places to store values are in cookies, sessions or a database. Which one you use depends on the exact scenario.

    http://www.w3schools.com/PHP/php_includes.asp
    http://www.w3schools.com/PHP/php_sessions.asp
  • Or, you can POST or GET the variable over.

    Like when you see something like http://example.com/?var=123

    That is available through a GET on another PHP file as a way to pass variables back and forth.
  • So, how would would I POST it, without creating a new form?

    Basically, the original issue I needed this for is solved. I tried sessions, but that was to confusing, and there were security issues, so I opted for cookies instead.

    But never-the-less, I am intrigued on how you would send a POST variable over to another php file without a form by actually POSTing it, if it is even possible?.

    (p.s. It's a shameful plug, i know :) but the site this thread was going to help is now completed. http://www.phpyouradmin.com/)
  • "mattvot" said:
    So, how would would I POST it, without creating a new form?


    PHP cURL:
    http://us3.php.net/curl


    I would learn sessions first though, as it's about 1/3 as complicated... I don't fully know cURL; I just hack my way through it when I need to. Lots of guessing and checking.