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).
Related
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.
This is the simple code which i uploaded to my web server to find why the header not working in the main application. Even this doesn't work i'm using the i page server. This works fin in the localhost wamp server.
<?php
ob_start();
header('Location: login-twitter.com');
ob_end_flush();
?>
I also tried the below code
<!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>Untitled Document</title>
</head>
<body>
<h1>Hello</h1>
<?php
ob_start();
header('Location: login-twitter.com');
ob_end_flush();
?>
</body>
</html>
try to use header('Location: http://www.login-twitter.com/'); instead of header('Location : login-twitter.com');
On the very start of the file, add following lines:
Even before the HTML <!Doctype>
<?php session_start();
ob_start();
?>
Note: Redirection will not occur if you have some HTML output on browser.
You must do the redirect ( header('Location: login-twitter.com'); ) before the html starting tag.
try this:
<script type="text/javascript">
window.location.assign('http://www.login-twitter.com');
</script>
I was learning PHP and trying to redirect pages based on if the cookie is set or not so below is the code I used to set the cookie on the first page
<?php
setcookie("test","logged in",time()+60,'/');
?>
Now on the testing page I delete the cookie but it doesn't get deleted below is the code
<?php
setcookie("test", 0, time()-(60*60*24*7));
if(isset($_COOKIE['test']))
{
echo "u had logged in";
}
else
header("Location: index.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>Untitled Document</title>
</head>
<body>
</body>
</html>
What exactly is the problem ?
Cookies are only set when a page is transmitted to the browser and are only read when they're sent to the server as a part of a HTTP request.
As such:
If you delete a cookie, it won't disappear until the next page load.
If you set a cookie, the value can't be read until the next page load.
A common way of dealing with this is to set/delete a cookie and then perform a re-direct.
Changes made to cookies are only visible to the server on refresh. If you reload the testing page, you shouldn't see the "logged in" text.
I have this php code:
<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!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>counter</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
clickMe
</body>
</html>
It's supposed to count the amount of times someone clicks on a certain link.
I saved this code in a file called index.php, and then in the same directory I made a file called counter.txt(set the permissions of counter.txt to 666). However when I run the script it comes up with:
Fatal error: Call to undefined function: file_put_contents() in /home/index.php on line 6
How can I fix this error, and somehow display the count click on the same page as the link?
If file_put_contents() is undefined, my guess is you're using a version of php < 5.
If that is the case, you will need to replace that function with fopen(), fwrite() and fclose(), see also the php manual page.
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()?