Ayuda con la función IF en PHP

  • Autor Autor ringo1979
  • Fecha de inicio Fecha de inicio
R

ringo1979

Curioso
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola. Estoy intentando que, al ejecutar la función IF, PHP me redireccione a la dirección correspondiente, dentro de la cual quiero incluir una variable que tendrá el mismo valor en todas las líneas (es decir, que, al redireccionar, sustituya "$variable" por "valor"):

<body><?php
$secondofyear=date("U");
$variable=valor;

if ($secondofyear<1385528400) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/1/$variable.html" />';
elseif ($secondofyear<1385614800) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/2/$variable.html" />';
elseif ($secondofyear<1385701200) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/3/$variable.html" />';
elseif ($secondofyear<1385787600) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/4/$variable.html" />';
elseif ($secondofyear<1385874000) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/5/$variable.html" />';
elseif ($secondofyear<1385960400) echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/6/$variable.html" />';
?>

¿Se puede hacer? Gracias.
 
Yo soy muy nuevo en PHP, pero me parece que el código va al "head" y no al body, como decis.

Lo has probado y no te funciona?
Por lo que veo, el código cambiaría el meta-tag del sitio, pero eso no asegura que redireccione.
Deberías usar otra función por ejemplo refresh, para que abra el sitio web que corresponde.
 
Si que se puede, mira:


PHP:
<body><?php
$secondofyear=date("U");
$variable=valor;

if ($secondofyear<1385528400){
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/1/' .$variable.html .'" />';
}elseif ($secondofyear<1385614800){ 
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/2/' .$variable.html .'" />';
}elseif ($secondofyear<1385701200){ 
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/3/' .$variable.html .'"" />';
}elseif ($secondofyear<1385787600){
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/4/' .$variable.html .'" />';
}elseif ($secondofyear<1385874000){ 
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/5/' .$variable.html .'" />';
}elseif ($secondofyear<1385960400){
  echo '<meta http-equiv="refresh" content="0; URL=http://www.misitioweb.es/6/' .$variable.html .'" />';
}
?>

Aunque te recomendaria hacer la redireccion con PHP.

Un saludo
 
Sí me redirecciona, pero a la dirección que está escrita, sin hacer la sustitución.
 
No confies en las redirecciones de JavaScript.
Prueba:
PHP:
<body><?php
$secondofyear = date("U");

$variable = 'valor';


function redireccionar($url) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $url");
    exit;
}

    
switch ($secondofyear) {
    case ($secondofyear < 1385528400):
        redireccionar('http://www.misitioweb.es/1/' . $variable . '.html');
        break;
    case ($secondofyear < 1385614800):
        redireccionar('http://www.misitioweb.es/2/' . $variable . '.html');
        break;
    case ($secondofyear < 1385701200):
        redireccionar('http://www.misitioweb.es/3/' . $variable . '.html');
        break;
    case ($secondofyear < 1385787600):
        redireccionar('http://www.misitioweb.es/4/' . $variable . '.html');
        break;
    case ($secondofyear < 1385874000):
        redireccionar('http://www.misitioweb.es/5/' . $variable . '.html');
        break;
    case ($secondofyear < 1385960400):
        redireccionar('http://www.misitioweb.es/6/' . $variable . '.html');
        break;
}
    

?>
 
Gracias a todos. Me funciona la solución de gomhermar. Probaré también la de shadowhck
 
Algo más eficiente:

PHP:
<body><?php
$secondofyear = date("U");

$variable = 'valor';

function redireccionar($url) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $url");
    exit;
}

// $var < *
$lista = array(
1385528400 => 'http://www.example.com/1/' . $variable . '.html',
1385614800 => 'http://www.example.com/2/' . $variable . '.html',
1385701200 => 'http://www.example.com/3/' . $variable . '.html',
1385787600 => 'http://www.example.com/4/' . $variable . '.html',
1385874000 => 'http://www.example.com/5/' . $variable . '.html',
1385960400 => 'http://www.example.com/6/' . $variable . '.html'
);
    
foreach ($lista as $key => $val) {

    if ($secondofyear < $key) {
        redireccionar($val);
        break;
    }

}
?>
 
PHP:
<?php
$secondofyear = date("U");
$variable = 'valor';
if($secondofyear < 1385528400){
	$url = 'http://www.misitioweb.es/1/'.$variable.'.html';
}elseif($secondofyear < 1385614800){
	$url = 'http://www.misitioweb.es/2/'.$variable.'.html';
}elseif($secondofyear < 1385701200){
	$url = 'http://www.misitioweb.es/3/'.$variable.'.html';
}elseif($secondofyear < 1385787600){
	$url = 'http://www.misitioweb.es/4/'.$variable.'.html';
}elseif($secondofyear < 1385874000){
	$url = 'http://www.misitioweb.es/5/'.$variable.'.html';
}elseif($secondofyear <1385960400){
	$url = 'http://www.misitioweb.es/6/'.$variable.'.html';
}
?>
<meta http-equiv="refresh" content="0; URL=<?=$url?>" />



Y si quieres redireccionar instantáneamente.

PHP:
<?php
$secondofyear = date("U");
$variable = 'valor';
if($secondofyear < 1385528400){
	$url = 'http://www.misitioweb.es/1/'.$variable.'.html';
}elseif($secondofyear < 1385614800){
	$url = 'http://www.misitioweb.es/2/'.$variable.'.html';
}elseif($secondofyear < 1385701200){
	$url = 'http://www.misitioweb.es/3/'.$variable.'.html';
}elseif($secondofyear < 1385787600){
	$url = 'http://www.misitioweb.es/4/'.$variable.'.html';
}elseif($secondofyear < 1385874000){
	$url = 'http://www.misitioweb.es/5/'.$variable.'.html';
}elseif($secondofyear <1385960400){
	$url = 'http://www.misitioweb.es/6/'.$variable.'.html';
}

header('Location: '.$url);
exit;
?>

Yo lo pondría así, más ordenado jaja 🙂
 
Última edición:
Atrás
Arriba