-3

I'm trying to use refresh as a redirect function because header does not work for some reason. Here is piece of my code, when I run it refresh loops:

----------NOT WORKING /---------------

$url = $_SERVER['REQUEST_URI'];
$url = substr($url, 10); //cuting url to "index.php?site=page"
echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';

----------WORKING /---------------

$url = 'index.php?site=page';
echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';

----------WHOLE FUNCTION /---------------

elseif($action=="nfvi") {
        safe_query("UPDATE ".PREFIX."nfv SET nfv=nfv+1");
    $url = $_SERVER['REQUEST_URI'];
    $url = substr($url, 10);
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';
}
qerigan
  • 51
  • 6

2 Answers2

1

Why not use HTTP headers?

<?php
$url = "http://www.google.com";

header("Location: index.php?site=" . urlencode($url)); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

See the manual.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • @shapeshifter Probably. But doesn't seem like OP knows what that means. – Kermit Feb 22 '13 at 01:20
  • i tried to use: header("Location: index.php?site=".$url.""); - not working – qerigan Feb 22 '13 at 01:21
  • Read this, http://stackoverflow.com/questions/2832010/what-is-output-buffering then turn output buffering on, send ur header and see if it works. – shapeshifter Feb 22 '13 at 01:23
  • If I can't use double quotes twice so how can I use header with a variable ??? lol – qerigan Feb 22 '13 at 01:26
  • 1
    @qerigan I said before, answer updated. See answer. Why you no read? – Kermit Feb 22 '13 at 01:27
  • header("Location: index.php?site=".$url); <- no effect, im still on old page – qerigan Feb 22 '13 at 01:29
  • what do you mean "your code broke" ? My code is fine i guess, all i have to do is to run function that changes a value in database and refresh the page. – qerigan Feb 22 '13 at 01:32
  • Try put ob_start(); at the very start of your code. And try rerun it. – shapeshifter Feb 22 '13 at 01:33
  • 1
    @qerigan It may be beneficial to future visitors if you told us what your problem was. – Kermit Feb 22 '13 at 01:38
  • Well it was pretty stupid of me because I didn't noticed that when i called the function url has changed to 'index.php?site=page&action=nfvi' so i changed 'substr($url, 10);' to 'substr($url, 10, -12);'. Anyway I still have problem cuz when its not 'index.php?site=page' but for example 'index.php?site=page3' user is redirected to 'page' insted of 'page3' if you know what I mean ... – qerigan Feb 22 '13 at 01:47
  • @qerigan just do a string replace, $url = str_replace("/~folder/", "", $url); – shapeshifter Feb 22 '13 at 01:51
1

You can use CURL as initiated as a client:

It will balance CURL_SET TIMEOUT: curl_setopt($ch, CURLOPT_TIMEOUT, 3);

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ping_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, trim($request)); $result = curl_exec($ch); curl_close($ch);

Are you check for refreshing page in header if header location not working?

 header("Refresh: 2;");

If header not working, use fsockets for reading a data:

$host = 'www.example.com';
$service_uri = '/cgi-bin/processACT';
$vars ='code=22&act=TEST';

$header = "Host: $host\r\n";
$header .= "User-Agent: PHP Script\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($vars)."\r\n";
$header .= "Connection: close\r\n\r\n";

$fp = pfsockopen("ssl://".$host, 443, $errno, $errstr);
if (!$fp) {
   echo "$errstr ($errno)<br/>\n";
   echo $fp;
} else {
    fputs($fp, "POST $service_uri  HTTP/1.1\r\n");
    fputs($fp, $header.$vars);
    fwrite($fp, $out);
    while (!feof($fp)) {
    echo fgets($fp, 128);
    }
    fclose($fp);
} 

And than make a loop with sleep(3) second with what you want for pause.

Why you are using meta tag instead of header in PHP?

header('Location: '.$url);

Or full header redirect example:

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53