Why does declaring objects voids the HTML in my Ajax page? - php

Background: I am building an Ajax page which will be called by a user's browser to retrieve data already loaded in $_SESSION. I want to test that the Ajax page works properly so I am trying to do a print_r($_SESSION) on it. (Note: the print_r() works fine on the main site.)
Problem: I cannot get my PHP to produce valid HTML. If I do:
<?php
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
if (!session_id()) {
session_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <!-- See http://www.w3.org/International/O-charset.en.php-->
<title>Test for Ajax</title>
<link rel="stylesheet" media="screen" type="text/css" title="StyleSheetProjet" href="StyleSheetProjet.css" />
</head>
<body>
<pre>
Content of $_SESSION:
<?php print_r($_SESSION) ?>
</pre>
</body>
</html>
In which case the page shows:
Content of $_SESSION:
Array
(
followed by a bunch of __PHP_Incomplete_Class Object (because the objects in session have not been declared I assume).
However if I start with:
header("Content-Type: text/html; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8" ?>';
//Obects in session:
require_once("Class_User.php");
require_once ('Class_Message.php');
if (!session_id()) {
session_start();
}
//(Rest is same as above)
Then the browser renders nothing at all, not even Content of $_SESSION:. In fact, the html in the web console is simply <html><head></head><body></body></html> with nothing in between...
What am I doing wrong?
Edit 1: to replace text/xml in the header() with text/html.
Edit 2: I replaced the two require_once with:
if (file_exists('Class_User.php')) {
echo "Class_User exists";
} else {
echo "Class_User doesn't exist";
}
if (file_exists('Class_Message.php')) {
echo "Class_Message exists";
} else {
echo "Class_Message doesn't exist";
}
The page does return Class_User existsClass_Message exists so it clearly can find the files.

Suspected Reason
require_once("Class_User.php"); and require_once("Class_Message.php"); are looking for the related files, cannot find them, so stop executing the rest of the code. Check if the path to Class_User.php and Class_Message.php are correct.
Actual Reason (we found working together w/ the OP after debugging)
One of the class files was extending another class and since that file was not included in the project the execution was being blocked. The OP solved the issue by calling another require() for this third class.

Related

$_POST variables not available on pages without HTML headers

I used to be able to post variables to pure PHP pages, but it seems to have been broken by a server setting change with our web host, because this used to work just fine. Basically, I've set up a form to retrieve and allow the user to download a file from a secure location outside of the public folder on the server (only to authorized users). It works like this:
request_file.php
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request File</title>
</head>
<body>
<form name="request_file" method="post" action="retrieve_file.php" enctype="multipart/form-data">
<input type="text" name="filename" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
retrieve_file.php
<?php
session_start();
header("Vary: User-Agent");
header("Pragma: public");
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=".$_POST['filename']);
readfile( "../../".$_SESSION['userid']."/".$_POST['filename'] );
?>
However, if I replace the retrieve_file.php code with simply this:
<?php
echo "<pre>";
var_dump( $_POST );
echo "</pre>";
?>
It will print out an empty array, every time. However, if I put HTML headers on retrieve_file.php, it works just fine. However, for exporting a file, this won't really work since the headers have to be sent out AFTER the page gets its post variables.
Any ideas how to work around this? Please don't berate me for pulling files this way, it actually works well for our very specific application.
Thanks!
Additional Information
PHP Version: 5.4
You need to access with the $_FILES variable, like this:
$_FILES["file"]["filename"]
Check more at http://www.w3schools.com/php/php_file_upload.asp

Force document end in PHP

I am trying to flush output to the browser and then disconnect the user on the MAC level. However, my script never actually finishes loading the browser, it shows loading but the meta refresh wont become active until this page actually finishes. Are their any functions I can do that will finish the output and let PHP do its own thing with out hanging on to the user.
<?php
if (ob_get_level() == 0) {
ob_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Loading... </title>
<meta http-equiv="refresh" content="6;URL='nexturl.php'" />
</head>
<body>
<p>Loading... (lots of whitespace here)</p>
</body>
</html>
<?php
ob_flush();
flush();
ob_end_flush();
sleep(1);
// This function kills their connection on their computer instantly
disconnect_user_on_their_router();
?>
To terminate, but still buffer, you can call these (in this order):
flush();
ob_flush();
exit();
However, I'm not sure how this would interact with your router-killer (which you'd need to call before exit).

How can i execute a link in a php page

I have a test.php page
in this page , there is a url . i need to execute that url (here database updation is doing).This is my code
<?php
$username ='testUsername';
if($_GET['age']!=''){
header('location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username); //need to updatethe age of this username
$show ='hello';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $show ?>
</body>
</html>
i know this code is not going to work properly.How can i write in a good way.I donot want to redirect the page.I just need to execute that link
Just call file_get_contents on the url in question instead of setting the location header to that value.
Since the update.php file is hosted on an external website, the only thing you can do is get the contents of the output of the file. (Use file_get_contents to get that output.) That is, it will call the file (with your parameters) and you can fetch the HTML result of it—nothing more. It would be a major security problem if server files could be executed on external websites.
If you need to include the code in update.php without redirecting, you can do so with the include or require functions.
require() is identical to include()
except upon failure it will ... halt
the script whereas include() only
emits a warning (E_WARNING) which
allows the script to continue.
Use the IMG tag. <img widht=0 height=0 src="<?='location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username ?>" />

php include not working on IE?

This include is not working in IE:
<?php
include_once 'localization.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Global Colleague</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/><!--Start Kampyle Exit-Popup Code-->
<script type="text/javascript">
Calling an array inside localization.php
<?php echo l('content_p3'); ?>
EDIT: I tried the same file in another folder and it worked
"Not working" is very generic. What is it that you expect and what is actually happening? You may want to turn error reporting on and see if any error is reported. In (X)HTML, nothing should be printed before the doctype. Are you trying to add something to the HTTP response? Typically, the browser shouldn't effect how PHP outputs your code unless you've added some code to respond to the user agent which is not always wise.
Perhaps, when you tried your code in another directory it wasn't able to find the offending script as it is included by a relative path. Try removing the include in the original file and see if it "works".
The following way 100% work for you:
1) Install (Open Source) Notepad++ On your PC
2) Open the file in it
3) Go to encoding and select (Encode in UTF-8 Without BOM)
4) Then click save
5) Now it can work on (Chrome) and (IE)
:)

Simple PHP Sessions Error

I have actually discovered my problem but I am really want to know why this is an issue. I had two pages form1.php I started a session on that page and hit submit. Then I had a link to session2.php which started that session and was able to pull the information from form1.php. I am just learning about sessions and this was a very simple exercise to learn what a session can do.
Here lies the issue, I had a stylesheet link in my head and it had a blank href well it was href="#" and when that was there the session2.php would not start the session from form1.php and grab the info from the form. Without that href="#" in the style tag it worked fine, and it also worked fine if it was a fake styletag href="something.css" but href="" doesn't work either.
Why is this? I only have those in because its a template I made for workflow, maybe I cant include the css link in my template anymore to prevent future issues.
You can see this site working here, if I haven't explained myself.
form1.php
<?php
session_start();
$_SESSION['name'] = $username;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<!--//CSS STYLESHEETS//-->
<link rel="stylesheet" href="#" type="text/css">
</head>
<body>
Go to session 2
<!--form stuff is in here-->
</body
session2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
session_start();
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Your second page needs to look like this:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Note that session_start() must appear before any content is printed to the screen.
Per the note on the session_start PHP manual page:
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
To work like you want it, you need to have started the session first. Sounds simple, because it is. When you say session_start, php then looks for an accepted session cookie first to process content.
From http://php.net/manual/en/function.session-start.php
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
Are you trying to output stuff to the page before you send the headers? What happens if you put the stylesheet after you call session_start()?

Categories