$_SESSION don't work in include file in php - php

My website has a very simple structure base on include in php.
Here the structure:
<?
session_start();
include("lang/".$_SESSION['lang'].".php");
?>
<html>
<body>
...
<? include("menu.php"); ?>
...
</body>
</html>
Content of menu.php is:
<div>
My name is <?= $_SESSION['name']; ?>
</div>
My problem is in the menu.php, the $_SESSION don't work anymore.
Is there a reason for that ?
Thanks.

You are not printing the name. That is why it is not shown.
Try this
<div>
My name is <?php echo "$_SESSION['name']"; ?>
</div>

Related

Why isn't my session variable working for second web page?

I was studying sessions and came up with a problem. On my first page, index.php, I wrote a simple code to start a session, given as follow:
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION['name'] = 'Daniel';
echo $_SESSION['name'];
?>
<ul>
<li>index</li>
<li>contact</li>
</ul>
</body>
</html>
Next, I created a file contact.php as follow:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION['name'];
?>
This is contact page.
</body>
</html>
In contact.php the session variable $_SESSION['name']; is not displayed. But in the first file, index.php, the session variable is set and is being displayed.
What's wrong here? Why isn't the session variable in contact.php being displayed?

Accessing php variables at different places in the same file

Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.

Passing Variable to other PHP without refreshing

I am new to PHP. I have these 3 files :
index.php
functions.php (to organize functions)
header.php
I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:
header.php
<!Doctype html>
<html lang="en">
<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.
functions.php
<?php
function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}
?>
Now, how do I allow this index.php to have custom page title instead of the default given by header.php?
Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?
I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.
I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php
EDIT :
Here I included my index.php
<?php
require 'functions.php';
?>
<?php
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.
header.inc
…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…
index.php
<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome
another-page.php
<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content
If you want to use a function, give it parameters.
function get_header($title = 'Some default title') {
…
}
the included file will have access to the variables in the function's scope.
in the functions.php
function get_header(){
if (file_exists('header.php'))
require 'header.php';
else echo "There is an error retrieving a file";
}
in the header.php, and in the balise title you call the session parameter
<!Doctype html>
<html lang="en">
<head>
<title>
<?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else 'Learn PHP'; ?>
</title>
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
and in the index.php
<?php
session_start();
$_SESSION['title-page'] = 'this is the welcome Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
and in another-page.php
<?php
session_start();
$_SESSION['title-page'] = 'this is an another Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>

PHP include code shows one thing but browsers show another

I have a series of php includes on a web page that look like this...
<?php include 'folder1/file.php'; ?>
<?php include 'folder2/file.php'; ?>
<?php include 'folder3/file.php'; ?>
They look like the above in my code editor but when I upload to my server it views like this when I look at the source code...
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
<?php include 'folder3/file.php'; ?>
So I thought maybe the file wasn't uploading....so I changed the code to this and uploaded the page....
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
And when viewing that in any browser it viewed as expected, showing just the two includes. Telling me that the web page is uploading properly to the server. I've emptied my cache, refreshed the page and nothing. So then when I change the code back to this and upload....
<?php include 'folder1/file.php'; ?>
<?php include 'folder2/file.php'; ?>
<?php include 'folder3/file.php'; ?>
It stills shows this in the live page
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
<?php include 'folder3/file.php'; ?>
I have PHP installed on the server and all other PHP codes/includes work correctly. The following PHP code does run correctly.
<?php phpinfo(); ?>
Here is the code for folder2/file.php
<div class="propertylistingbox" id="propertylistingbox">
<div class="proplistingleft" id="proplistingleft"><img src="/second-base/images/FirstBase01.png" alt="Second Base Exterior" width="284" height="193" border="0" />
<div class="reservation-request-button">
</div>
</div>
<div class="propertylistingmain" id="propertylistingmain">
<div class="propheader" id="propheader">
<h3>SECOND BASE</h3>
</div>
<div class="propbulletlist" id="propbulletlist">
<ul>
<li>Located In Oneonta, NY</li>
<li>Accommodates 4</li>
<li>High Speed Internet</li>
<li>2 Bedrooms</li>
<li>Close to 3 bedroom apartment "First Base"</li>
<li>1.25 Miles to Cooperstown Baseball World</li>
<li>2.75 Miles to Cooperstown All Star Village</li>
<li>19 Miles to Cooperstown Dreams Park</li>
</ul>
</div>
<div class="propdetails" id="propdetails">
<div class="details-button">
Click For More Details
</div>
</div>
<div class="propspecialsbanner" id="propdetails2"></div>
<div class="propcalendarmaplink" id="propcalendarlink"><strong>CALENDAR OF AVAILABILITY</strong><br />
<strong>GOOGLE MAP</strong></div>
</div>
</div>

how to add standard elements in php in one line

I have created a few php files Following are their names:
standard_head.php (which contains basic standard html code)
header.php
navbar.php
sidebar.php
footer.php
standard_footer.php (which contains closing html tags)
Page content will vary in every page and would also include html content.
I am trying to create a lot of php pages which will use all of the above pages directly.
Now I can use them directly using include statement for each of them, but I was wondering if it was possible to include all of them together with one just statement ?
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
You could so this (note that this is a very basic example and that I wouldn't use this myself without taking into account meta tags, page titles etc.)
template.php
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $content; ?>
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
Then, on a page (say index.php, for example):
index.php
<?php
$content = '<h1>Welcome</h1>';
include 'template.php';
?>
P.S: If you go down this route, make sure that you check out the Output Control Functions first.
Just create a PHP file that has a list of them i.e.
<?php include("this file");
include("the other file");
?>
And then just add that file.
Not really an answer to your question, but you could use a template engine like Twig.
http://twig.sensiolabs.org/
Try using more complex template system like Smarty or some MVC framework like Zend (this is not required but would allow you to create complex sources more easily) and then build script like this:
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $menu->getCustomEntries(); ?>
</div>
Where $menu will be your custom object containing methods for displaying menus and submenus...
There is no straight way to include multiple files, as include / require functions except only one argument. though you can use following logic.
`#include_all_files.php
include('../includes/standard_head.php');
include('includes/header.php');
...
use above file in other files
include('includes/include_all_files.php');
`

Categories