PHP Session variables not preserved across pages - php

I'm new to PHP and I'm facing some problems when working with PHP sessions
Let's say I have a file (index2.php) with this code in it.
<?php
session_start();
$_SESSION['name'] = 'The User';
?>
Click
And this is index3.php
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
For some reason I don't understand, index3.php doesn't show anything. What am I doing wrong?
Thanks!

In index3.php you need to start the session as well. As per the official PHP docs:
When session_start() is called or when a session auto starts, PHP will
call the open and read session save handlers.
Using your example, just initiate session_start() as follows:
<?php 
session_start();
?>
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>

Make sure you also have session_start(); in all php pages where you want to retain and work with sessions;
make sure index3.php contains session_start();

Related

PHP header location not working- working in xampp

<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
<?php
header('Location: http://www.google.com');
?>
</body>
</html>
I uploaded this code to my server(1 and 1). Its not redirecting. I used the same code in XAMPP and it worked fine.
What am I doing wrong? Why is it working on xampp and not on real (1and1) server? I appreciate any help.
Thank you
You can not call header function after output. it shows warning as "header already sent...".
you must write it before tag.
<?php
header('Location: http://www.google.com');
?>
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
</body>
</html>
*may be your server setting for showing warning is off so it wont display warnings to you on related server.
If by some reason you can't place the php snippet before the html code (like Mayur said) you can use JavaScript for redirects
window.location.href = "http://stackoverflow.com";
Remember that when setting headers from php or setting cookies no text must be out putted before the php command!

How to change the value of a variable from the fontend in PHP?

I want to change the value of a variable from the front end. But I want this change permanently in file even if we reload this page the changed code should be executed. This is my code:
<?php $headingMain = 'Amazing Website'; ?>
<!DOCTYPE html>
<html>
<head>
<title>PHP PRACTICES</title>
</head>
<body>
<h2><?php echo $headingMain; ?></h2>
</body>
</html>
In this case I want to change $headingMain from front end.
BUT HOW?
you should enter the data in to a database and load it from there when needed,
other solution would be to write it in a text file but i wouldn't recommend it.
: http://se2.php.net/manual/en/book.mysqli.php

PHP include in a different place

I have the following markup:
<?php include 'admin/assets/includes/adminBoot.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Page</title>
<?php if($loggedIn){ include 'admin/assets/includes/editPageDochead.php'; } ?>
</head>
<body>
<?php if($loggedIn){ include 'admin/assets/includes/adminBar.php'; } ?>
....
What I'd like to know is, would it be possible to just have the first php include there, which has some script to place the other 2 php includes in the correct places?
It's part of a CMS I'm building and I want it to be easy enough to integrate by only needing 1 include on each page.
I don't want to have to add these 3 includes to everypage ideally.
You can have a header.php file with the content you've pasted in your question and then include just that one.
Why not have the include pages themselves check the logged in status?
<?php include 'admin/assets/includes/adminBar.php'; ?>
and in the files:
<?php
if($loggedIn)
{
// Do yout stuff here.
}
?>
They won't show anything unless the user is logged in... This way, you can simply include them wherever you need them and they display the data only as needed.

session_start() fails when loading php file from jquery

I am including a php file into JQuery, like this:
Main php file:
<?php
session_start();
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?php
$_SESSION['ID'] = "32";
?>
<div id="sesion"></div>
<script>
$("#sesion").load("fun.php");
</script>
php file that will load, fun.php:
<?php
session_start();
echo 'new php';
echo $_SESSION['ID'];
?>
fun.php shows blank as soon as I add session_start() if i remove it, it will display:"new php"
I need to open session in the "fun.php" file, but I can't seem to achieve it.
Put session_start() at the top of the page that is including chalk_element.php within it's page
Try simplifying your environment.
Create a new index.php with session_start() at the very top, then create a couple of session vars.
Next, create your chalk_element.php file and load it via jQuery in index.php.
Now do something very simple to access a session var and see what happens. This will give you a simplified test environment where you can experiment with the configuration.
It sounds like your outputting something (HTML) before the point you call session_start. You can either move session_start() so it's at the top of the page (right at the top, no whitespace) or call ob_start() first. You can then output data and call session_start() later.
Also, check your error logs for any useful error information as to why the PHP file has just stopped working.
Both files will require session_start(); at the top of each file before any other output as this function sends out several HTTP headers.
Here is a simple example:
index.php
<?php
//Start session
session_start();
//Set the value in your session
$_SESSION['key']='value';
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<script>
$(function() {
$("#Chalk").load('chalk_element.php');
});
</script>
<div id="Chalk">Loading...</div>
</body>
</html>
chalk_element.php
<?php
//Start session also to `resume` session
session_start();
//Check from ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
//output session var if set
echo isset($_SESSION['key']) ? $_SESSION['key'].' was set in session' : 'session value was not set';
}
?>
It was an encode problem when I saved the files. I saved both files in ANSI code and problem solved.
Try This One..!
ob_start(); //top of the page
ob_flush();//end of the page

PHP Include page content does not refresh

i have a serious problem over here. I created a user system with sessions.
The problem is that there is content you only see when you are logged in. For example in the navigation bar the sign in button is replaced with a account button.
Now to my problem:
Every page php-includes the navbar.php.
For example in the index.php is written:
<body>
<?php include("navbar.php")?>
</body>
The login.php redirects to the index.php:
header("Location: index.php");
But the index.php does not refresh. After a hard refresh with "F5" every thing is fine.
I also tried meta tags to prevent loading the page in the cache.
Any Ideas?
index.php:
<html lang="en">
<head>
...
</head>
<body >
<?php include("navbar.php")?>
<div id="wrap">
...
</div>
</body>
</html>
navbar.php
<div class="navbar">
<?php
session_start();
if (!isset($_SESSION['logged']) || !$_SESSION['logged'])
{?>
...Sign in etc...
<?php
}
else
{?>
...Accounting...
<?php
}?>
</div>
login.php:
<html>
<head>
</head>
<body>
<?php include("navbar.php"); ?>
<div class="container">
<form class="form-signin" action="logon.php" method="post" >
...
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
logon.php:
<?php
if login successfull //pseudo code
header("Location: http://www.***.com/index.php");
}
else
{
header("Location: http://www.google.de");
}
exit;
?>
add this to your .htaccess file. this will disable browser caching on these file extensions
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
According to the specs, you have to pass an absolute uri.
It's also best to add this, when redirecting:
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.google.com');
For SEO purpouses, and browser cache (google this to find out more).
I hope you know this already, but you'll also have to make sure no output has been sent to the client, because in that case, the headers have already been sent, and logic dictates that it's too late to change them, then.
Check this question for more details on how to deal with headers and output buffering.

Categories