سلام
من یک کد معادل این کد در php احتیاج دارم
response.redirect "index.htm"
خیلی متشکرم
سلام
من یک کد معادل این کد در php احتیاج دارم
response.redirect "index.htm"
خیلی متشکرم
<?php
header("Location: http://".$_SERVER['HTTP_HOST']
.dirname($_SERVER['PHP_SELF'])
."/".$relative_url);
?>
OR:
<?php
header("Location: http://www.yahoo.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
سلام
من از این کد استفاده کردم ولی این پیغام خطا را به من می دهد:
Warning: Cannot add header information - headers already sent by (output started at /home/virtual/site107/fst/var/www/html/send.php:2) in /home/virtual/site107/fst/var/www/html/send.php on line 69
سلام
کسی نیست که به من جواب بدهد؟
چرا نیست برادر من. این کد رو امتحان کن:
<?php
// If no headers are sent, send one
if (!headers_sent()) {
header ('Location: http://www.example.com/');
exit;
}
// An example using the optional file and line parameters, as of PHP 4.3.0
// Note that $filename and $linenum are passed in for later use.
// Do not assign them values beforehand.
if (!headers_sent($filename, $linenum)) {
header ('Location: http://www.example.com/');
exit;
// You would most likely trigger an error here.
} else {
print "Headers already sent in $filename on line $linenum\n" .
"Cannot redirect, for now please click this <a " .
"href=\"http://www.example.com\">link</a> instead\n";
exit;
}
?>
1- قبل از اجرای دستور هدر نباید هیچ گونه چیزی رو به خروجی فرستاده باشی.
2- میتونی از ob_... استفاده کنی
سلام
اینکه قبل از اجرای دستور هدر نباید هیچ گونه چیزی رابه خروجی فرستاده باشیم یعنی چی؟
متشکرم
یعنی این که اصلا نباید قبلش با دستور echo یا print یا ... پیامی رو به خروجی فرستاده باشی
این پیام خطا هم که میفرسته همون رو میگه
headers already sent by
ببین را حت ترین کار اینه که از java script استفاده کنی برای redirect
سلام
می توانید روش redirect را با javascript توضیح دهید یا یک مثال ساده بنویسید
با تشکر
اینم تابع redirect توی php:
// func: redirect($to,$code=307)
// spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
function redirect($to,$code=301)
{
$location = null;
$sn = $_SERVER['SCRIPT_NAME'];
$cp = dirname($sn);
if (substr($to,0,4)=='http') $location = $to; // Absolute URL
else
{
$schema = $_SERVER['SERVER_PORT']=='443'?'https':'http';
$host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
if (substr($to,0,1)=='/') $location = "$schema://$host$to";
elseif (substr($to,0,1)=='.') // Relative Path
{
$location = "$schema://$host/";
$pu = parse_url($to);
$cd = dirname($_SERVER['SCRIPT_FILENAME']).'/';
$np = realpath($cd.$pu['path']);
$np = str_replace($_SERVER['DOCUMENT_ROOT'],'',$np);
$location.= $np;
if ((isset($pu['query'])) && (strlen($pu['query'])>0)) $location.= '?'.$pu['query'];
}
}
$hs = headers_sent();
if ($hs==false)
{
if ($code==301) header("301 Moved Permanently HTTP/1.1"); // Convert to GET
elseif ($code==302) header("302 Found HTTP/1.1"); // Conform re-POST
elseif ($code==303) header("303 See Other HTTP/1.1"); // dont cache, always use GET
elseif ($code==304) header("304 Not Modified HTTP/1.1"); // use cache
elseif ($code==305) header("305 Use Proxy HTTP/1.1");
elseif ($code==306) header("306 Not Used HTTP/1.1");
elseif ($code==307) header("307 Temorary Redirect HTTP/1.1");
else trigger_error("Unhandled redirect() HTTP Code: $code",E_USER_ERROR);
header("Location: $location");
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}
elseif (($hs==true) || ($code==302) || ($code==303))
{
// todo: draw some javascript to redirect
$cover_div_style = 'background-color: #ccc; height: 100%; left: 0px; position: absolute; top: 0px; width: 100%;';
echo "\n";
$link_div_style = 'background-color: #fff; border: 2px solid #f00; left: 0px; margin: 5px; padding: 3px; ';
$link_div_style.= 'position: absolute; text-align: center; top: 0px; width: 95%; z-index: 99;';
echo "\n";
echo "Please See: ".htmlspecialchars($location)."
\n";
echo "\n\n";
}
exit(0);
}
همین جناب redirect رو توی manual پی اچ پی جستجو کن.با مثال خیلی خوب توضیح داده.
نکاتی در مورد استفاده از دستور header
https://barnamenevis.org/showth...light=ob_start
https://barnamenevis.org/showth...light=ob_start
https://barnamenevis.org/showth...light=ob_start
من از کد زیر استفاده میکنم
header('Location: http://www.example.com/');