Is there a way to make a variable be able to be used in another .php file?
E.G
In PHP1.php
$age="9";
$someOtherThing="1";

In PHP2.php
echo $age+$someOtherThing;

I have a statement to make, I hate being a n00b.

Recommended Answers

All 72 Replies

Member Avatar for diafol

Yes.
You have a few options. Web pages are 'stateless' which basically means no info can be stored unless you specifically pass it to another page or store it as some flavour of cookie. You can use 4 common methods:

$_GET variables can be used to retrieve querystring data (the stuff in the url following the '?').
$_POST variables can be used to retrieve form data.
$_COOKIES variables are used to read cookie data
$_SESSION variables are used to pass data within a 'session' - this is often the most useful if you don't want to use a form or have the data show up in the querystring.

$_REQUEST is often seen in tutorials - for Dog's sake, don't use this.

Javascript CAN be used to pass data via cookies - but this isn't reliable as users could have js turned off. Less people turn off cookies, but if they do $_COOKIES and $_SESSION are in trouble.

A more elaborate way to store data between pages is to use a database. 'Write to' in one page, 'read from' in another page. Sometimes session data is stored via DB - but this is probably more advanced than you could cope with at the moment.

Caveat
I'm no expert, just an enthusiastic hobbyist. Check out the online php manual. You can find a downloadable copy on the site (php.net).

Ah, yes, I have $name=$_POST["name"] //I have a <input name="name" .\ On PHP1.php and how do I do this on PHP2.php?
E.G
echo "Hello" . $_POST[$name]; ?

Member Avatar for diafol

yes - have you tried it?

Always a good idea to do a stripslashes() on $_POST.

If you're doing form processing, e.g: user submits form from PHP1.php and PHP2.php echoes "Hello (name)", then you'll want it like this.

Form on PHP1.php

<form action='PHP2.php' method='post'>
Name: <input type='text' name='name' /><br />
<input type='submit' value='Submit' />
</form>

Script on PHP2.php

<?php
if(isset($_POST['name'])) {
echo "Hello " . $_POST['name'];
}
?>

Use whatever you have under the name attribute on the input tag as the array key in $_POST.

Or are you talking about something else?

Yes, this is what I wanted but wouldn't that only work o PHP2? Is it possible to then get than name to PHP3 after going to PHP2?

I messed around a bit and got:
PHP1

<form action="PHPTEST2.php" method="post" name="sign up">
Name: <input type="text" method="post" name="name"><br />
Last Name: <input type="text" method="post" name="lastName"><br />
<input type="submit" value="Sign Up Now!">
</form>
<?php
$name=$_POST["name"];
$LastName=$_POST["lastName"];
?>

and in PHP2:

<?php
if(isset($_POST['name'])){
echo "Hello " . $_POST['name'] . " " . $_POST['lastName'] . " .";
}
?>

How could I make the 'name' into the variable $name and 'lastName' into the variable $LastName?
Also I want the variables to be able to be used in PHP1,2,3,4.

Let me explain how form processing works:

<form action="PHPTEST2.php" method="post" name="sign up">
Name: <input type="text" method="post" name="name"><br />
Last Name: <input type="text" method="post" name="lastName"><br />
<input type="submit" value="Sign Up Now!">
</form>

This form will send the information to PHPTEST2.php. You CANNOT access it before it is submitted and the new page loads. However: You can submit it to the same page. Additionally, if you want to store the user's name over multiple pages, you can use sessions. This is probably what you're looking for.
E.g:

PHP1.php

<?php
session_start();

if(isset($_POST['name']) ) {
$_SESSION['name'] = $_POST['name'];  
echo "Welcome, " . $_SESSION['name']; 
}
else {
echo "<form action='' method='post'>
<input type='text' name='name' />
<input type='submit' name='Submit' />
</form>";
}
?>

Once $_SESSION is assigned, you can use it in any script just by including session_start(); at the top(before any output).


PHP2.php(User must visit PHP1.php first)

<?php
session_start();

echo "Welcome " . $_SESSION['name'];
?>
Member Avatar for diafol

I still say that you should look up the tutorials and the php manual. Getting piecemeal help from forums won't help you develop your php skills. I intentionally did not include code so that you'd have to research it and produce it yourself.

'Sessions' as I mentioned originally would be your best bet if you wish to keep data 'live' between loads of pages.

Yes, this is what I wanted but wouldn't that only work o PHP2? Is it possible to then get than name to PHP3 after going to PHP2?

Hi,
you really deserve the piece of cake we always give on noob's birthday!

Thank you lsmjudoka, and everyone else who helped.
evstevemd, I went to learn PHP there before I came here, thanks for the reference though.

Okay, now session can't send cookie, is this something to do with my browser or do I have to code it to send a cookie?
Cookies == yum;

Is there a way to make a variable be able to be used in another .php file?
E.G
In PHP1.php
$age="9";
$someOtherThing="1";

In PHP2.php
echo $age+$someOtherThing;

I have a statement to make, I hate being a n00b.

If I'm right then this should be the answer to your question:

PHP2.php

require_once( 'PHP1.php' );
echo $age . $someOtherThing;

Also for the rest of you, he asked how he could use a variable in another file, not across sessions or browsers or anything like that, come on people, everything you do doesn't require a 2Meg file to accomplish, some things can actually be done in one or two lines if you remember.

Arionyx, I do not believe that is what he's trying to do. Different file in this case = different page. But you can ask...

TheSecOrg, are you getting a message like this?

"Warning - Cannot send cookie, session cache limiter already sent. Output started by PHP1.php at line 5"

If you are, there is something in your script that is sending output before you call session_start();
Make sure there are no spaces before your first <?php tag, and no echo or print statements before session_start();
Let me know if that fixes it.

Member Avatar for diafol

Okay, now session can't send cookie, is this something to do with my browser or do I have to code it to send a cookie?
Cookies == yum;

post your code and errors

I had Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\PHPTEST.php:4) in C:\xampp\htdocs\PHPTEST.php on line 5
and my code is

<html>
<head><title>PHP</title></head>
<body>
<?php
session_start();
if(isset($_POST['name']) ) {
$_SESSION['name'] = $_POST['name'];
header( 'location: http://localhost/PHPTEST2.php' );
}
else {
echo '<form action="PHPTEST2.php" method="post" name="sign up">
Name: <input type="text" method="post" name="name"><br />
Last Name: <input type="text" method="post" name="lastName"><br />
<input type="submit" value="Sign Up Now!">
</form>';
}

I tried deleting the <html> to the <body> stuff and no errors but it doesn't show the name after "Welcome ", my code for PHP2:

<?php
session_start();

echo "Welcome " . $_session['name'] . "." . '<br />';
?>

Sorry Ardav

<?php
session_start();
if(isset($_POST['name']) ) {
$_SESSION['name'] = $_POST['name'];
header( 'location: PHPTEST2.php' );
}
else {
echo '<html>';
echo '<head><title>PHP</title></head>';
echo '<body>';
echo '<form action="PHPTEST2.php" method="post" name="sign up">
Name: <input type="text" method="post" name="name"><br />
Last Name: <input type="text" method="post" name="lastName"><br />
<input type="submit" value="Sign Up Now!">
</form>';
}?>

It still only has the Welcome .
PHP2:

<?php
session_start();

echo "Welcome " . $_SESSION['name'] . "." . '<br />';
?>

<form action="PHPTEST2.php" method="post" name="sign up">
Name: <input type="text" method="post" name="name"><br />
Last Name: <input type="text" method="post" name="lastName"><br />
<input type="submit" value="Sign Up Now!">

Correct this form first. It contains errors. Also Namke your files. I failto see which is php1 and which is phpxx. It is good behaviour to use meaningful names than phptestxxx

Sorry evstevemd, it still doesn't work though.

Post your new code in this format

myfile1.php

<?php
//Code for MyFile1.php goes here
?>

myfile2.php

<?php
//Code for MyFile2.php goes here
?>

Double posting...ooughh!

That is what I've done, still doesn't work.
PHP1

<?php
session_start();
if(isset($_POST['name']) ) {
echo '<h1>LOL FORUM</h1>';
$_SESSION['name'] = $_POST['name'];
header( 'location: signUp.php' );
}
else {
echo '<html>';
echo '<head><title>PHP</title></head>';
echo '<body>' . '<font face="comic sans ms">';
echo '<h1>LOL FORUM</h1>';
echo '<form action="signUp.php" method="post">
Name: <input type="text" name="name"><br />
Last Name: <input type="text" name="lastName"><br />
<input type="submit" value="Sign Up Now!">
</form>';
}
?>

PHP2

<?php
session_start();

echo "Welcome " . $_SESSION['name'] . "." . '<br />';
?>
Member Avatar for diafol

It may be that $_POST is empty.

try echoing out $_POST - this will stop the header() command, but at least you'll know if the variable actually has any data in it:

<?php
session_start();
if(isset($_POST['name']) ) {
echo "post var: " . $_POST['name'];
$_SESSION['name'] = $_POST['name'];
echo "<br />session var: " . $_SESSION['name'];
//header( 'location: signUp.php' );
...

I usually check for the submit field name. You haven't set one here:

<input type="submit" value="Sign Up Now!">

I suggest this:

<input type="submit" name="subsignup" id="subsignup" value="Sign Up Now!" />

Now you test for the sent form (as opposed to a field) - then within this piece of code, you validate all form fields. Send errors out to the original form page if any fields are empty etc.
If the tests are passed, go to your destination page.

BTW - it's not good practice to send a form to itself since reload/refresh can resend the info. Your form should send the data to a formhandler page, which does the processing/sort out the session data etc and then return to a suitable page via header().

Hm, $_POST works but $_SESSION doesn't, gah, what to do?

Member Avatar for diafol

have you disabled cookies on your browser? sounds like you can't store the data to the session file.

perhaps it may be worth renaming $_SESSION to $_SESSION and seeing if that works.

Still doesn't work :( .

Try making PHP2 like this:

<?php
session_start();

if(isset($_SESSION['name']) ) {
     echo "Welcome " . $_SESSION['name'] . "." . '<br />';
}
else {
     echo "No name in session!";
     exit;
}

?>

If this shows "No name in session!" even when you've loaded PHP1.php and echoed the $_POST + set $_SESSION = $_POST;, then it's clearly a problem with your sessions. Additionally, what is your error_reporting set to? If something's truly wrong with the sessions, I'd expect an error message to come up. But your configuration may be stopping it.

Still doesn't work :( .

I would suggests couple of probing techniques:
1. Try on different browser in same machine
2. Reinstall WAMP/LAMPP
3. Check browser settings

If you are using IE, M$ have its bad ways of blocking things (most of times is JS) so check using option 3. If it doesn't work, try it on another machine to confirm that you have give problem. Also turn on error reporting.

Tried on Google Chrome and Opera, I don't even have WAMP/LAMPP, browsers allow all cookies and don't block any settings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.