Ok, maybe my brain is just shut off, but I can't get this to work.
Here's the complete code:
Page1.php:
<?php
$something = "hello";
include "Page2.php";
?>
Page2.php:
<?php
echo $something;
?>
Desired output (when navigating to Page1.php):
hello
The real output is blank. I have tried putting the global keyword everywhere, and nothing happens. Am I missing something?
I cannot replicate this error, just tried this on my localhost and copied and pasted your code from here. I suspect you have some sort of syntax error.
Turn on error reports and see if you get any errors.
I know this is a late answer, but I'm trying to do something similar. First of all, when you echo something you still have to put it in " ". Php will recognize it as a variable as long as you put the $.
Second, you're including page2.php in page1. Fantastic, but page2 does not recognize $something. Now, if you do it the other way, declare $something in page2, and then call it from page 1 after including it, it will run.
Modifying the variable would require something else...
I think the output is coming in page2.php . Am i right? this is because you are echoing a unset variable in page2.php you need to change the following data in order to make it work.
page1.php
<?php
include("page2.php");
echo $something;
?>
page2.php
<?php
$something="Hello";
?>
If you will use it and navigate the page 1.php then the output will be Hello
I had a similar problem running on local (Windows) where the values of an array did not follow past the include within the same process.
After switching the include path from http://localhost/www/example.php to C:/www/example.php, it works fine now.
Related
I have a php file
index.php and its url is index.php?var=item
I defined get variable in index.php
in index.php
<?php
require "included.php";
$var=$_GET['var'];
?>
I echoed this variable in my included.php like below
in included.php
<?php
echo $var;
?>
When I launch index.php?var=item, its shows an error that var is not defined in included.php?
How to overcome this? I want to define some variables in index.php from url and do some staff in included file.
like Joachim Martinsen posted as a comment (I don't know why he hasn't just answered), you have to set the $var before including your file:
<?php
$var=$_GET['var'];
require "included.php";
?>
inlcuding in PHP basically just works like concatenating one file out of other files. so your original code would result in:
<?php
echo $var;
$var=$_GET['var'];
?>
which obviously doesn't work because the variable is echoed before it is getting a value assigned.
Any variables set up in your PHP code before including another file would work there. The included file won't be aware of any other variable that was declared or set after it was included.
In index.php, do this:
<?php
if(isset($_GET['var'])) {
$var=$_GET['var'];
}
require "included.php";
?>
Then access the $var value like you are already trying to do in included.php.
And as I am writing this answer, I can see that a similar answer has just been posted. Sorry for posting it anyway, I think it includes a bit more explanation.
I’m pretty much a complete beginner when it comes to PHP and have been having some problems with displaying a Welcome Username message on a HTML page and where to place session_start(); on the page.
Here’s the relevant part of the HTML page: can one place PHP directly into the page like this?
<div class="col-sm-4">
<h2><p>Welcome, <?php echo $_SESSION['Username']; ?>.</p></h2>
Right at the top of the page (before the HTML) I’ve also put:
<?php
session_start();
?>
<!doctype html>
(is this session start in the right place?)
Not sure if I’m going about this in the right way though :/
Thank you very much for your time; very much appreciate it as well as other helpful replies here.
EDIT Everything works once I changed the pages extension to PHP rather than HTML
can one place PHP directly into the page like this?
Yes. Gathered you use the <?php ?> tags, like you did, and the file has the .php extension; you can.
(is this session start in the right place?)
Also correct.
You haven't said what error you're receiving but I'm assuming you'll have a undefined index 'Username' error (if you don't, turn ON php error reporting and you'll probably see it) this happens because, unless you set the value previously, your 'Username' index inside the $_SESSION array was not defined by $_SESSION['Username'] = "Mosh Mage"
Hope this helps :)
Your code is correct but never forget to store the user name to $_SESSION['username'] or else your it will echo nothing. Before you can store user information in your PHP session, you must first start up the session.
Note: The session_start() function must appear BEFORE the html tag: in your case correct as well.
School yourself with sessions at http://www.w3schools.com/php/php_sessions.asp
OK.. forgive me, there are a couple similar questions posted already with the correct responses... I knew someday I would have to ask the people of the internet for help but I never thought it would be something so stupid...
here is my code.. so far..
index.php:
include "http://www.mywebsite.com/shared.php";
$page = "homepage";
include "http://www.mywebsite.com/htmlheader.php"; //trying to use "page" variable in here
echo ("<br>test2: " . $_SESSION['test']);
include "http://www.mywebsite.com/htmlfooter.php";
shared.php:
session_start();
$_SESSION['test'] = "what the f_ck im scared";
htmlheader.php:
echo ("test1: " . $page . "<br>" . $_SESSION['test']);
Output right now is:
test1:
test2:
(so the pages are being included.. just not able to use the variables..) From what I understand in its current state this should be printing something like:
test1: what the f_ck im scared
test2: homepagewhat the f_ck im scared
..The funny thing is I was not having any issue with the include using the variable. I had added some things but then it randomly stopped working so I reduced it down to this to try and figure out what the problem was.. I am assuming I have made some silly mistake.
Ensure that you have started the session with session_start();
also check whether allow_url_fopen or allow_url_include or both have been set to 0 (disabled) in php.ini. if yes try to activate it
include "htmlheader.php";
instead of
include "http://www.mywebsite.com/htmlheader.php";
ensure you have session_start(); at the top of the page that is including the includes
I'm experiencing a strange PHP session issue. Can someone tell me if that's how session works?
To see the problem, load the following code into any php file, say test.php, and run it 2 times. NOTE, you have to run it two times, i.e. load the page and reload it.
<?
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
On my server, the first time I load this test page, I get
Original////Original////
and that is correct. But when I reload it, I get
Original////New////
which means the 5th line "$test="New////";" actually rewrite my $_SESSION["test"]. That doesn't make sense to me. Anyone knows what is happening? Or it's just happening on my server???
Seems like register_globals is enabled on your server. You'll need to disable the directive.
Firstly, do not use <? as a starting tag of PHP, please use <?php. Secondly, this is an expected behavior if you have register_globals enabled. Look at this link:
http://www.theblog.ca/session-register-globals
It's title says:
When register_globals is on, session variables overwrite global variables
And sample code is similar to yours:
<?php
session_start();
$canadaday = 'July 1st';
$_SESSION['canadaday'] = 'July 2nd';
print '<p>When is Canada Day?</p>';
print '<p><strong>' . $canadaday . '</strong></p>';
?>
With register_globals, result is July 2nd. HTH.
<?php
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
I have tried your code in my environment its running perfectly.its always print Original////Original////
so its happening only on your server
I have a PHP script called customers.php that is passed a parameter via the URL, e.g.:
http://www.mysite,com/customers.php?employeeId=1
Inside my customers.php script, I pick up the parameter thus:
$employeeId = $_GET['employeeId'];
That works just fine. I also have an HTML file with a form that inputs the parameter, and runs another php script, viz:
<form method="post" action="listcustomers.php">
Employee ID: <input name="employeeId" type="text"><br>
<input type="submit" value="Show">
</form>
Then in my listcustomers.php script, I pick up the parameter so:
$employeeId = $_POST['employeeId'];
So far so good. But his is where I am stuck. I want to run my customers.php script, and pass it the parameter that I pave picked up in the form. I am sure this is really simple, but just cannot get it to work. I have tried:
include "customers.php?employeeId=$employeeId&password=password";
But that does not work. Nor does anything else that I have tried. Please help me.
You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['employeeId'] is defined before you call:
include 'customers.php';
Then customers.php will also have access to $_GET['employeeId'];
PHP includes are really includes; they attach piece of code from that location to this location.
PaulPRO’s answer most likely tells what you want to achieve, but also other variables would be usable in the included file. So if you define $foo = "bar"; in index.php and include layout.php after that line, you could call this $foo variable in that layout.php file.
If you seriously want to pass an URL to the file, you can insert the URL into variable and in included file parse it with parse_url
EDIT: and yes, I do not suggest adding query params to actual include url as it starts to mess up with GET array and eventually you’ll just be confused what there should be and start smacking your head into wall.
Your include file will have access to the GET variables directly.
You can use $GLOBALS to solve this issue as well.
$tit = "Hello";
$GLOBALS["docTitle"] = $tit;
include ("my.php");
You can pass the value through Session
Like
<?php
$_SESSION['name']='peter';
$_SESSION['age']=28;
include('person_info.php');
?>
In person_info.php, start the session and put all session value in the variable and unset the session.
This is not so clean but this will work if you need to enter a static parameter:
if (true){
echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=red%20bull' />";
}
else
{
echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=blue%20bull' />";
}
Just put it within your source code for the page which calls the query_source.php.
Once again, it is NOT very clean...