session_start() fails when loading php file from jquery - php

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

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!

redirect in php in the middle of the site

I have a site in PHP; I want to redirect to external site after running some code; so I want something in the body tag; to redirect it after my code running; something like following example ; what can i do?
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
</header>
<?php
$myffirend=" ";
$allmyffirend=" ";
$user=$_GET['user'];
// this info save in my db
redirect here // after save the information
other code
?>
You can inject javascript script with redirection:
echo "<script>window.location = 'http://www.yourdomain.com'</script>";
If you want redirect after page load
if (window.addEventListener) {
window.addEventListener("load", afterLoadRedirect, false);
} else {
window.attachEvent('onload', afterLoadRedirect);
}
function afterLoadRedirect(){
window.location='your url';
}
It's not posible to redirect using PHP after printing some code.
I suggest to move the code to the start of the file.
If this is not possible, you could do it using javascript:
window.location='your_url.php';
You can try the refresh meta tag (seconds, followed by destination):
<meta http-equiv="refresh" content="5; url=http://example.com/" />
The W3C recommends against using it for this purpose but I'm assuming you want to briefly show some sort of message before it changes. If not, stick with changing the Location header which must be done before any output:
header('Location: destination.php');
Use javaScript inside php:
echo '<script> window.location = "http://www.yoururl.com";</script>';

PHP Session variables not preserved across pages

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();

How to change title of a page when headers are already sent through PHP

I have a couple of PHP pages which begins with a secure session start. When I put a page title in the head section it says "headers already passed". How can I put page titles while securely starting a PHP function at the beginning of the page?
If you are using session_start() Place your php script before any HTML code.
You cannot use or have an output before session_start() not even an echo.
e.g.
<?php
session_start();#creates a session or resumes the current
include(lib/inc.php);
?>
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<head>
<body>
<p>text</p>
<body>
</html>
An other solution if you must have an output is to use ob_start http://php.net/manual/en/function.ob-start.php

Is there a standards-compliant way to start a PHP session and echo a JavaScript script in one include() statement?

I have two scripts that I call with two PHP include() calls. The first starts a session / sets cookies, the second loads one of two JavaScript scripts. To keep things valid, I've been using the two calls but I'd like to just combine them into one.
Current setup (simplified):
<? include "session.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<? include "scripts.php" ?>
...
What I'd like:
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
...
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
So which is better: echo the DOCTYPE, use include() twice, or use include() once and have invalid markup?
EDIT - The whole script (session and javascript) should ideally be fully implementable with one line of code (e.g. the one include())
Use ob_start at first to avoid problems with session_start
<?php ob_start();?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "session_and_scripts.php"; ?>
A way that uses only 1 1file and no additional instructions:
<?php include "session_and_scripts.php" ?>
<!-- more head-stuff-->
</head>
<body>
<!--more content-->
session_and_scripts.php should do the following:
<?php
//do the session stuff
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//some javascript
</script>
(But I would'nt say it's a good approach)
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
Assuming that you do not want to have a valid markup, there is no problem, the only restriction is that session_start is called before any kind of "echo"...
Assuming you want a valid markup using only one include and without echoing the DOCTYPE from the included file, you can save the script text into a php variable and echo it in the main page after the inclusion
//main page
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<?php echo $script;?>
// session_and_scripts.php
<?php
session_start();
$script = '<blablabla>';

Categories