PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ehsun7b
Forum Newbie
Posts: 8 Joined: Tue Nov 22, 2005 12:33 am
Contact:
Post
by ehsun7b » Sat Oct 28, 2006 11:24 am
I use this function to redirect to other pages in the directory.
Code: Select all
function redirect($page) {
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/$page");
exit;
}
And will use it when th user post some data from the form like this:
Code: Select all
if (isset(_POST['ok'])) {
redirect("~tutorial.php?a=s"); // action = sent/saved
exit();
}
But I got this message on the server:
Warning: Cannot modify header information - headers already sent by (output started at /home/ehsun7b/public_html/~tutorial.php:27) in /home/ehsun7b/public_html/php/myUtils.php on line 73
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Oct 28, 2006 11:26 am
please use the forum search, use
headers already sent as search term.
short answer: you cannot send http headers after any content is sent to the client.
output started at /home/ehsun7b/public_html/~tutorial.php:27
Line 27 of ~tutorial.php generates output before header() is called.
ehsun7b
Forum Newbie
Posts: 8 Joined: Tue Nov 22, 2005 12:33 am
Contact:
Post
by ehsun7b » Sat Oct 28, 2006 11:46 am
Can you explain more, I can't find anything fine in the forum, Is it possible at all to redirect a posted page???
nickvd
DevNet Resident
Posts: 1027 Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:
Post
by nickvd » Sat Oct 28, 2006 11:47 am
Click on the button that says search at the top of the page.
In the box where it says "search for keywords" type in: headers already sent
Click search.
You'll find what you're looking for...
ehsun7b
Forum Newbie
Posts: 8 Joined: Tue Nov 22, 2005 12:33 am
Contact:
Post
by ehsun7b » Sat Oct 28, 2006 1:00 pm
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
There is no additional whitespace before my session_start() What is the problem??? It works in a simple sample page, but in my main page it does not work!Code: Select all
<?php
//-----------my utility functions
function redirect($page) {
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/$page");
exit;
}
?>
This is the main page:
Code: Select all
<?php session_start();
// requires
require_once('./php/config.inc.php');
require_once('./php/myUtils.php');
require_once('./fckeditor.php');
// fill navigations array
$navDoc = simplexml_load_file('./xml/adminnavigation.xml');
foreach ($navDoc->a as $a) {
$link["href"] = $a->href;
$link["target"] = $a->target;
$link["title"] = $a->title;
$link["hint"] = $a->hint;
$navigationLinks[] = $link;
$link;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>:: Ehsun Behravesh Personal Website ::</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/main.css" type="text/css" />
<link rel="stylesheet" href="css/link.css" type="text/css" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<link rel="stylesheet" href="css/admin.css" type="text/css" />
<style type="text/css">
/*<![CDATA[*/
/*]]>*/
</style>
</head>
<?php
// connect DB if required
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(mysqli_connect_errno() && $WEBSITE_MODE == MODE_DEBUGE) {
echo "<div class='error'>\n";
printf("Connect failed: %s\n", mysqli_connect_error());
echo "</div>";
}
?>
<body>
<div class="header" style="background-image: url(images/adminheader.gif)">
</div>
<table class="layout" cellpadding="0" cellspacing="0">
<tr>
<td class="left">
<?php
// NAVIGATION
foreach ($navigationLinks as $link) {
echo "<a class='navigation' href='".$link['href']."' title='".$link['hint']."'>".$link['title']."</a>";
}
?>
<div class="leftgroup">
<div class="lefttopic">News Letters</div>
<?php
$sql = "SELECT count(*) as count FROM newsletter";
if (!$result = $mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
$newsCount = 0;
} else {
$record = $result->fetch_assoc();
$newsCount = $record['count'];
}
echo $newsCount.' Newsletter emails';
?>
</div>
<div class="leftgroup">
<?php
$total_vote = 0;
$excellent = 0;
$good = 0;
$notbad = 0;
$bad = 0;
$terrible = 0;
$sql = "SELECT count(*) as count FROM vote";
if (!$result = $mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
} else {
$record = $result->fetch_assoc();
$total_vote = $record['count'];
}
$sql = "select vote, count(*) as count from vote
GROUP BY vote
order by vote";
if (!$result = $mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
} else {
while ($record = $result->fetch_assoc()) {
switch ($record['vote']) {
case 1:
$excellent = $record['count'];
break;
case 2:
$good = $record['count'];
break;
case 3:
$notbad = $record['count'];
break;
case 4:
$bad = $record['count'];
break;
case 5:
$terrible = $record['count'];
break;
}
}
}
?>
<div class="lefttopic">Vote Results</div>
<?php
echo '<table class="form" style="color: #AAA; margin-left: auto; margin-right: auto">';
echo '<tr>';
echo '<td class="caption">';
echo 'Excellent:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo round($excellent * 100 / $total_vote).'%';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption">';
echo 'Good:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo round($good * 100 / $total_vote).'%';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption">';
echo 'Not bad:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo round($notbad * 100 / $total_vote).'%';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption">';
echo 'Bad:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo round($bad * 100 / $total_vote).'%';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption">';
echo 'Terrible:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo round($terrible * 100 / $total_vote).'%';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption" colspan="2">';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="caption">';
echo 'Total:';
echo '</td>';
echo '<td class="data" style="color: #FFFFFF">';
echo $total_vote. ' votes';
echo '</td>';
echo '</tr>';
echo '</table>';
?>
</div>
<div class="leftgroup" style="">
<div class="lefttopic">Visitors</div>
<?php
$counter = 1;
$sql = "SELECT count(*) as count FROM counter";
if (!$result = $mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
$counter = 0;
} else {
$record = $result->fetch_assoc();
$counter = $record['count'];
}
echo "<span style='font: normal normal 12px times new roman'>".$counter."</span>";
?>
</div>
</td>
<td class="right">
<div class="right">
<?php
if ((!isset($_SESSION['behravesh_user']) || !isset($_SESSION['behravesh_user_encoded'])) || $_SESSION['behravesh_user_encoded'] != md5($_SESSION['behravesh_user'])) {
echo "<table width='100%'>";
echo '<tr>';
echo '<td>';
echo "You did not log in!";
echo '</td>';
echo '<td>';
echo "<div class='exit'><input type='button' class='button' value='Enter' onclick='document.location=\"~login.php\"'/></div>";
echo '</td>';
echo '</tr>';
echo '</table>';
} else {
echo "<table width='100%'>";
echo '<tr>';
echo '<td>';
echo '<h3 class="title">Tutorial</h3>';
echo '</td>';
echo '<td>';
echo "<div class='exit'><input type='button' class='button' value='Exit' onclick='document.location=\"~logout.php\"'/></div>";
echo '</td>';
echo '</tr>';
echo '</table>';
if (isset($_GET['a']) && $_GET['a'] == 'd') {
$id = $mysqli->escape_string($_GET['id']);
$sql = "DELETE FROM tutorial WHERE id = '$id'";
if (!$mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
} else {
echo "Deleted successfully!";
}
}
$sql = "SELECT * FROM tutorial ORDER BY id DESC";
if (!$result = $mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
} else {
echo '<div>'.$result->num_rows.' tutorials</div>';
if ($result->num_rows > 0) {
?>
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<td class="title">ID</td>
<td class="title">Title</td>
<td class="title">Language</td>
<td class="title">Programming language</td>
<td class="title">Visit Times</td>
<td class="title">Action</td>
</tr>
<?php
while ($record = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>";
echo $record['id'];
echo "</td>";
echo "<td>";
echo $record['title'];
echo "</td>";
echo "<td>";
echo getDomainTitle($dlanguage, $record['lang']);
echo "</td>";
echo "<td>";
echo getDomainTitle($dplanguage, $record['plang']);
echo "</td>";
echo "<td>";
echo $record['visittimes'];
echo "</td>";
echo "<td>";
echo "<a href='~tutorial.php?a=d&id=".$record['id']."'>Delete</a> / ";
echo "<a href='~tutorialedit.php?id=".$record['id']."'>Edit</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<?php
}// num_rows > 0
if (isset($_POST['_ok'])) {
$title = htmlspecialchars($mysqli->escape_string($_POST['_title']));
$lang = htmlspecialchars($mysqli->escape_string($_POST['_lang']));
$plang = htmlspecialchars($mysqli->escape_string($_POST['_plang']));
$content = $_POST['content'];
$sql = "INSERT INTO tutorial (title, lang, plang, content)
VALUES('$title', '$lang', '$plang', '$content')";
if (!$mysqli->query($sql)) {
echo "<div class='error'>\n";
printf("Query failed: %s\n", mysqli_error($mysqli));
echo '<br />';
echo $sql;
echo "</div>";
} else {
redirect("~tutorial.php?a=s"); // action = sent/saved
exit();
}
}
if (isset($_GET['a']) && $_GET['a'] == 's') {// action = sent
echo "Saved successfully!";
}
$content = "";
$basePath = '';
$oFCKeditor = new FCKeditor('content') ;
$oFCKeditor->BasePath = $basePath;
$oFCKeditor->Value = $content ;
$oFCKeditor->Height = "700px";
//$oFCKeditor->Config['SkinPath'] = $basePath.'/editor/skins/silver/';
$oFCKeditor->Config['AutoDetectLanguage'] = false ;
echo "<div style='margin-top: 10px'>New tutorial:</div>";
echo "<form method='POST' action='' enctype='multipart/form-data' >";
echo "<table class='form' style='width: 100%'>";
echo "<tr>";
echo "<td class='caption'>Title: ";
echo "</td>";
echo "<td class='data'>";
echo "<input type='text' class='text' id='title' name='_title' maxlength='200' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='caption'>Language:";
echo "</td>";
echo "<td class='data'>";
echo "<select name='_lang'>";
foreach ($dlanguage as $language) {
echo "<option value='".$language['value']."' >".$language['caption']."</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='caption'>P. lang:";
echo "</td>";
echo "<td class='data'>";
echo "<select name='_plang'>";
foreach ($dplanguage as $planguage) {
echo "<option value='".$planguage['value']."' >".$planguage['caption']."</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='caption'>Content: ";
echo "</td>";
echo "<td class='data'>";
$oFCKeditor->Create() ;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='caption' colspan='2'>";
echo "<input type='submit' class='button' name='_ok' value='Add' />";
echo "<input type='reset' class='button' name='_clear' value='Clear' />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
?>
</div>
</td>
</tr>
</table>
<div class="footer">
<div class="textnavigation">
<?php
// text navigation
$i = 0;
foreach ($navigationLinks as $link) {
$i++;
echo "<a class='textnavigation' href='".$link['href']."' title='".$link['hint']."'>".$link['title']."</a>";
echo ($i == count($navigationLinks) ? "" : " | ");
}
?>
</div>
<div class="contactus">
<a href="mailto: info@behravesh.ws">info@behravesh.ws</a>
</div>
</div>
<br /><br /><br />
<div class="w3c">
<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml11"
alt="Valid XHTML 1.1" height="31" width="88" /></a>
</p>
</div>
</body>
<?php
// disconnect DB if connected
$mysqli->close();
?>
</html>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
wyrmmage
Forum Commoner
Posts: 56 Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID
Post
by wyrmmage » Sat Oct 28, 2006 1:03 pm
*sighs* did you not look at the posts above yours? Anyway, you must use the ob_start(); function (preferable after you use session_start().
-wyrmmage
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Oct 28, 2006 1:07 pm
Warning: Cannot modify header information - headers already sent by (output started at /home/ehsun7b/public_html/~tutorial.php:27) in /home/ehsun7b/public_html/php/myUtils.php on line 73
not session_start() is causing the warning but some code in myUtils.php.
~tutorial.php:27 <- output
myUtils.php on line 73 <- header()
wyrmmage
Forum Commoner
Posts: 56 Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID
Post
by wyrmmage » Sat Oct 28, 2006 3:42 pm
no, I know....
Code: Select all
<?
session_start();
ob_start();
echo('
<HTML>
<HEAD>
<TITLE>
</HEAD>
<BODY>
header("Location: index.php");
</BODY>
</HTML>
');
This will not throw a compile error because you are using ob_start(), but this will:
Code: Select all
<?
session_start();
echo('
<HTML>
<HEAD>
<TITLE>
</HEAD>
<BODY>
header("Location: index.php");
</BODY>
</HTML>
');
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Oct 28, 2006 3:57 pm
It's not a compiler error, it 's runtime
wyrmmage wrote: Anyway, you must use the ob_start(); function (preferable after you use session_start().
What good does a buffer do after the header-after-contents error occurs?
nickvd
DevNet Resident
Posts: 1027 Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:
Post
by nickvd » Sat Oct 28, 2006 5:12 pm
We've told you what the problem is... many times... I think it's time to go buy a book...
wyrmmage
Forum Commoner
Posts: 56 Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID
Post
by wyrmmage » Sat Oct 28, 2006 11:43 pm
volka wrote: It's not a compiler error, it 's runtime
wyrmmage wrote: Anyway, you must use the ob_start(); function (preferable after you use session_start().
What good does a buffer do after the header-after-contents error occurs?
No no no, you start the buffer before ANY of your script executes (except perhaps session_start()), that way ALL of your stuff gets put into the buffer, which is then sent to the browser when the script is finished executing. If php finds a redirection in your code, it just redirects the browser and does not send the buffer to the browser for viewing.
If you really feel the need to not ue ob_start(), then you could just echo() some javascript, like this:
Code: Select all
<?
echo('
<SCRIPT TYPE="text/javascript">
window.location("whatever.php");
</SCRIPT>
');
?>
I'm not quite sure if the javascript syntax is exactly correct, but you get the point...
-wyrmmage
chakhar86
Forum Commoner
Posts: 45 Joined: Mon Jun 05, 2006 1:36 am
Contact:
Post
by chakhar86 » Sun Oct 29, 2006 1:16 am
Hey, I have got same problem.
But in my case, redirection run completely fine in my localhost or LAN, but it echoed similiar error as mentioned if I run my code on internet. (Is it hosting problems?)
what's wrong?
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sun Oct 29, 2006 3:00 am
wyrmmage wrote:
No no no, you start the buffer before ANY of your script executes (except perhaps session_start()), that way ALL of your stuff gets put into the buffer, which is then sent to the browser when the script is finished executing.
Not completely true. The ob_* stuff will not buffer the output generated http headers... Like it or not, if you're planning to use the http location header you'll have to make sure you actually know what your code does... Blindfully relying on ob*_ doesn't help...