473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

passing variable from one php file to another?

Dear All,

I am wondering how to pass one variable from one php file to another
continuously or discontinuously.
For example, variable $a is declared in file1.php. I like to use that
variable in file2.php or file3.php where in file2.html, I won't use
that variable.
Looking forward to your help.

Thanks for your time and experience.

Jul 17 '05 #1
3 20992
Ko Ko wrote:
I am wondering how to pass one variable from one php file to another
continuously or discontinuously.


Files do not contain variables at all. They are just code on a disk, they
are static.

What you are referring to is either some kind of process (a file in
execution) or a session (a sequence of processes started by the same user).
In fact, usually the kind of question you ask is referring to sessions, and
PHP happens to have functions for that.

Try assigning your variable $a to $_SESSION["a"], and then look into the
contents of $_SESSION["a"] in the next "file".

Also, read up on http://php.net/session for more information on session
handling in PHP.
<?php
# I am file1.php
session_start(); # read up on session.auto_start
$_SESSION["a"] = 17;
?>

<?php
# I am file2.php
session_start(); # read up on session.auto_start
echo $_SESSION["a"];
?>

Kristian

Jul 17 '05 #2
Ko Ko wrote:
Dear All,

I am wondering how to pass one variable from one php file to another
continuously or discontinuously.
For example, variable $a is declared in file1.php. I like to use that
variable in file2.php or file3.php where in file2.html, I won't use
that variable.
Looking forward to your help.

Thanks for your time and experience.


Hi,

You can pass variables from one PHP script to another in different ways.

1) use SESSION
If you use SESSION, you can easily store a value in it and retrieve it from
another PHP script.
script1.php
// sets and stores the variable
$a = 42;
$SESSION["myNumber"] = $a;

From script2.php:
echo "myNumber is $a";

2) POST the value to script2.php
If script1.php does NOT produce any output, and just want to call
script2.php, you can post to script2.php.

Remember that sccript1.php should stop doing anything that is relevant after
redirecting the browser to script2.php

You can also use querystring to pass information to script2.php (which is
easier)

From script1.php:
$a = 42;
$newLocation = "http://www.yoursite.org/script2.php?a=$a";
header ("Location: $newLocation");
exit;

From script2.php:
$a = "unknown";
if (isset($_GET["a"])) {
$a = $_POST["a"];
}

The above example uses $_GET. $_GET is called a superglobal and it contains
all name/value pairs found on the url (after the ?).

Note that if you need/want to pass information using POST, you must send a
form with method="POST", or use CURL. (See www.php.net for details)
In that case the superglobal $_POST will contain all name/value pairs.

Hope this helps.

Good luck!

Regards,
Erwin Moller
Jul 17 '05 #3
Dear Friends,

Thanks for your contributions.

It really help me to the way.

With Regards,

Ko Ko

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
4
by: thomasp | last post by:
In the code below I have a function that tests if a file exists. It takes a variable named strFileName, simple enough. My question is, is there a way to pass it a variable with another name as...
3
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
8
by: chimambo | last post by:
Hi All, I am trying to pass variables between two frames in PHP. I want to get a variable from F1 to F2 and the variable that is now in F2 should pass back the variable to F1 and call another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.