Variables and session variables not working in php includes - php

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

Related

Header doesn't work in combination with session_start?

To avoid illegal use I check the login status at the beginning of the code. I do this as follows:
if (!isset($_SESSION['loggedin'])){
header('Location:http://www.name.nl/prg/login.php');
exit();
}
This works. But if I use this code first it doesn't work.
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');
session_start();
if (!isset($_SESSION['loggedin'])){
header('Location:http://www.name.nl/prg/login.php');
exit();
}
Am I missing something? I looked for 2 days now, but can't find a reason/solution. It seems to me that the header function should work after ini_set and session_start. I mean it is common code?
Try this first: ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']).'/name.nl/tmp'));
(Move parentheses)
Try looking here: http://php.net/manual/en/function.session-save-path.php
Not sure if this will help or not but it seems related to where your session folder is at and then pointing to the proper directory/folder in your code.
The solution that worked for them:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
Your current session save:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');
Well I found the cause. I had an "echo statement" before the header statement. After deleting this statement it works fine.
Thx for your comment anyway!

Echo Username welcome message in HTML page with PHP

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

How to integrate a constant in Redirection Header of php

It is defined in a config.php file
define("url_money", "www.himanshu1234.net63.net/MoneyManager/");
it is included in some other file like
include_once './config.php';
and used like this
<?php
header('Location: '.url_money.'login.php');
}
?>
there is some problem in it but unable to sort out.. !!
Change
define("url_money", "www.himanshu1234.net63.net/MoneyManager/");
to
define("url_money", "http://www.himanshu1234.net63.net/MoneyManager/");
When you define some link without http:// then it will be treated as relative link. So you were actually redirected to /www.himanshu1234.net63.net/MoneyManager/
I had an issue like this, and I made a stupid mistake. Then I saw
It is important to notice that header() must be called before any actual output is sent
So, please double-check whether there is any echo or printf, etc... costed me a few hours some years ago!

Changing PHP session variable

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

PHP global variables across files

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.

Categories