La web funciona con una variable url llamada action, depende del valor un switch mostrará un contenido un otro, este contenido son funciones, que a su vez muestra n, por citar algo, artículos, menús, comentarios, etc.
Ahora estoy creando una de login, pero en lugar de crear una función he creado un archivo, login.php, que es llamado mediante un include. El contenido del archivo es este:
login.php
Código PHP:
<?php require('Connections/SCMS.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
?><?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "privilegy";
$MM_redirectLoginSuccess = "?action=default&login=true";
$MM_redirectLoginFailed = "?action=login&error=1";
$MM_redirecttoReferrer = false;
mysql_select_db($database_SCMS, $SCMS);
$LoginRS__query=sprintf("SELECT username, password, privilegy FROM users WHERE username=%s AND password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));
$LoginRS = mysql_query($LoginRS__query, $SCMS) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = mysql_result($LoginRS,0,'privilegy');
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?><!-- start content -->
<div id="content">
<div class="post">
<h2 class="title">Iniciar Sesión</h2>
<p class="byline"><small>Escrito por SCMS 2<a href="#"></a></small></p>
<div class="entry">
<form action="<?php echo $loginFormAction; ?>" method="POST" name="login">
<p>Nombre de usuario:<br/>
<input name="username" type="text">
</p>
<p> Contraseña: <br/>
<input name="password" type="password">
</p>
<p>
<label></label>
<br />
<input type="submit" name="button" id="button" value="Iniciar sesión" />
</p>
</form> </div>
<p class="links">
<?php if (isset ($_GET['error'])) {
switch ($_GET['error']) {
case 1:
echo ' <a href="?action=login" class="more">Error, has introducido datos erróneos</a>
';
break;
}
} else { } ?>
<a href="#" class="comments">Todos los campos son obligatorios</a></p>
</div>
</div>
<!-- end content -->
test2.php
Código PHP:
<?php include('header.php'); ?>
<?php site(); ?>
<?php footer(); ?>
Código PHP:
<?php echo_site('footer-text'); ?>
</body>
</html>
Código PHP:
function site()
{
switch($_GET['action']) {
case 'search':
search();
break;
case 'login':
include ('login.php');
break;
case 'register':
include ('register.php');
break;
case 'logout':
include ('logout.php');
break;
case 'contact':
break;
case 'about':
break;
case 'default':
if (isset($_GET['id'])) {
mode_show($_GET['id']);
comment($_GET['id']);
} else {
if (isset($_GET['cat'])) {
category($_GET['cat']);
break;
} else {
echo "INDEX";
}
}
break;
}
}
Código PHP:
<?php include('core.php');
session_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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="themes/<?php echo_site('theme'); ?>/default.css" rel="stylesheet" type="text/css" media="screen" />
<title><?php echo_site('name'); ?> »
<?php
switch($_GET['action']) {
case 'search':
echo "Buscar";
break;
case 'login':
echo "Iniciar Sesión";
break;
case 'register':
echo "Registrarse";
break;
case 'contact':
echo "Contacta";
break;
case 'about':
echo "Sobre";
break;
case 'default':
if (isset($_GET['id'])) {
echo_pages('title', $_GET['id']);
} else {
if (isset($_GET['cat'])) {
echo_categories('title', $_GET['cat']);
} else {
echo "Inicio";
}
}
break;
default:
if (isset($_GET['id'])) {
echo_pages('title', $_GET['id']);
} else {
if (isset($_GET['cat'])) {
echo_categories('title', $_GET['cat']);
} else {
echo "Inicio";
}
}
break;
}
?>
</title>
</head>
<body>
<!-- start header -->
<div id="header">
<div id="logo">
<h1><a href="?action=default"><?php echo_site('name'); ?></a></h1>
</div>
<?php searchform(); ?>
<div id="menu">
<ul>
<?php $selected = 'class="current_page_item"'; ?>
<li <?php if ($_GET['action'] == 'default') { echo $selected; } else {} ?>><a href="?action=default">Inicio</a></li>
<?php if (isset($_SESSION['MM_Username'])) { ?><li <?php if ($_GET['action'] == 'logout') { echo $selected; } else {} ?>><a href="?action=logout">Cierra Sesión (<?php echo $_SESSION['MM_Username']; ?>)</a></li><?php } else {} ?>
<?php if (!isset($_SESSION['MM_Username'])) { ?>
<li <?php if ($_GET['action'] == 'login') { echo $selected; } else {} ?>><a href="?action=login">Inicia Sesión</a></li><?php } else {} ?>
<?php if (!isset($_SESSION['MM_Username'])) { ?>
<li <?php if ($_GET['action'] == 'register') { echo $selected; } else {} ?>><a href="?action=register">Registrarse</a></li>
<?php } else {} ?>
<li <?php if ($_GET['action'] == 'contact') { echo $selected; } else {} ?>><a href="?action=contact">Contacta</a></li>
<li <?php if ($_GET['action'] == 'about') { echo $selected; } else {} ?>><a href="?action=about"> Sobre</a></li>
</ul>
</div>
</div>
<!-- end header -->
<!-- start page -->
<div id="page">
No logro iniciar sesión (login.php no funciona), y no sé ni por qué ni cómo arreglarlo.
Si llamo mediante include a login.php en test2.php (sustituyendo a site() por include('login.php')) SÍ que funciona login.php, así que deduzco que el fallo está en site() o en alguna de las funciones a las que llama.
Las funciones no las pongo porque son más de 500 líneas de código, y me parece una burrada pornerlo aquí, ya que liaría más que otra cosa.
Si login.php funciona bien el fallo está en site() sin duda, si necesitáis el código de cualquier función, pedirlo.
Muchas gracias por anticipado
EDITO: He estado probando, login.php falla cuando uso un switch, por ejemplo, si uso un switch sólo con case 'login' también falla, así que no es por otra función, sinó por el switch.
ARREGLADO: Aviso, ya lo he arreglado, sólo he pasado el código PHP de la página de login.php a test2.php, justo antes de la función site(), que la he cambiado por el switch, eliminando una función que sólo se usa una vez.