pass variable to another php file

Posted on 2003-11-06
Last Modified: 2008-03-17
php newbie here,

i need to pass the a variable (e.g. $filename) from file1.php to file2.php. Now I know I can include file1.php in file2.php and simply say $newfile = $filename.

But if file1.php is performs some processing operations, I don't wanna include it in file2, coz it (file1.php) will get executed again. Is there a way to send the $file to file2.php without having to include file1.php.

Question by:skylabel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 13

Expert Comment

ID: 9698576
if you have a link to file2.php then you can just do file2.php?filename=wee and access this variable through $_GET["filename"] on file2.php

if you have a form then you just add a hidden input, i.e.

<input type="hidden" name="filename" value="wee">

this can be accessed through $_GET["filename"] if your method is GET and $_POST["filename"] if it's POST



Author Comment

ID: 9698800
thanks, but actually i want file1.php to send the variable $filename directly to file2.php without the user having to click on a link. So maybe a hidden form field in file1.php?

correct me if i'm mistaken....
$filename is dynamic and it's value is defined in file1.php already. So I can't define it in the form.

In file1.php, i'll include something like this?

<form action="file2.php" method="get" name="form">
<input type="hidden" name="filename"></input></form>

In file2.php
echo $_GET["filename"]

please clarify....thanks
LVL 13

Expert Comment

ID: 9698841
can you tell me the purpose of file1? if you want to go straight to file2 without the user doing anything, use the header function:

header("Location: file2.php?filename=$filename");

the above redirects to file2 with the filename in the $_GET variable, but you can only use this if no actual HTML content has been output to the screen.. so no print/echo commands before the header function and you can't have an html template output either

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.


Author Comment

ID: 9699069
ok, file1 is actually a file upload script and file2 is a form to mail script containing the filename of the uploaded file. I tried a dummy echo "hello world"; and it works (i.e. the email script displays after the file upload) ... so I think the hidden form field idea would work...but I'm getting parsing errors for that. Can someone see where the error is

Basically I need PHP to render the following html
<form action="uploademail.php" method="get" name="form">
<input type="hidden" name="filename"></input></form>

This is what I've got (which has errors i think). Can someone correct the following...

echo "<form action=\"uploademail.php\"method=\"get\"name=\"form\"><input type=\"hidden\" name=\"filename\"></input></form>";

LVL 13

Expert Comment

ID: 9699078
echo "<form action=\"uploademail.php\" method=\"get\" name=\"form\"><input type=\"hidden\" name=\"filename\"></input></form>";

Accepted Solution

krozz56 earned 110 total points
ID: 9704878

// proccesses

$_SESSION[file] = "test.php";

header("location: file2.php");


echo $_SESSION[file];

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question