Buen día,
Estoy iniciando en la programación PHP, para esto he instalado el APACHE 2.2, PHP 5.3.3 y MySQL 5.5. y estoy tratando de programar un entorno de INTRANET con el fin de autocapacitarme y especializarme en este lenguaje y este entorno.
Apenas estoy con la sección del LOGIN donde tengo un detalle, de algun modo la variable que declaro como $_SESSION no esta guardando el valor que se supone estoy pasando y esto me genera un "Access Denied"...
Incluyo el codigo del PHP se supone valida: "sessionauth.php"
<?php
//Start session
session_start();
//Include database connection details
require_once('cfgdb.php');
//Array to store validation errors
$MsgErr_arr = array();
//Validation error flag
$MsgErr = false;
//Connect to mysql server
$db_lnk = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$db_lnk) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
echo "Login ID missing";
$MsgErr_arr[] = 'Login ID missing';
$MsgErr = true;
}
if($password == '') {
echo "password ID missing";
$MsgErr_arr[] = 'password missing';
$MsgErr = true;
}
//If there are input validations, redirect back to the login form
if($MsgErr) {
$_SESSION['MsgErr_ARR'] = $MsgErr_arr;
session_write_close();
header("location: http://localhost:8081/appweb/php/login/loginform.php");
exit();
}
//Create query
$Qry="SELECT * FROM tblusers WHERE nameuser='$login' AND passuser='$password'";
$ExecQry=mysql_query($Qry);
//Check whether the query was successful or not
if($ExecQry) {
if(mysql_num_rows($ExecQry) == 1) {
//Login Successful
session_regenerate_id();
$usersauth = mysql_fetch_array($ExecQry);
$_SESSION['SESS_ID_USER'] = $usersauth['id_user'];
$_SESSION['SESS_NAME_USER'] = $usersauth['nameuser'];
do {
echo key($_SESSION);
echo "<br />";
echo current($_SESSION);
echo "<br />";
} while(next($_SESSION));
session_write_close();
header("location: indexauth.php");
exit();
} else {
//Login failed
header("location: authfailed.php");
exit();
}
} else {
die("Query failed");
}
?>
Cuando el usuario y la contraseña son validos se llama un 3er PHP que es donde va a comenzar a desplegarse lo que el usuario podrá acceder mediante el: "indexauth.php":
<?php
require_once('authlogon.php');
?>
<!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" />
<title>Member Index</title>
<link href="http://localhost:8081/appweb/css/loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <?php echo $_SESSION['SESS_NAME_USER']; ?></h1>
<a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a>
<p>This is a password protected area only accessible to members. </p>
</body>
</html>
Como requiero saber si el usuario sigue conectado, me auxilio del "authlogon.php"
<?php
//Start session
session_start();
//Check whether the session variable SESS_ID_USER is present or not
if(!isset($_SESSION['SESS_ID_USER']) || (trim($_SESSION['SESS_ID_USER']) == '')) {
header("location: accessdenied.php");
exit();
}
?>
Que en este ultimo es donde me doy cuenta que no guarda el valor la $_SESSION... pudieran decirme donde tengo el error.
Gracias de antemano por su apoyo, Saludos.
Oscar Carrizales.
Estoy iniciando en la programación PHP, para esto he instalado el APACHE 2.2, PHP 5.3.3 y MySQL 5.5. y estoy tratando de programar un entorno de INTRANET con el fin de autocapacitarme y especializarme en este lenguaje y este entorno.
Apenas estoy con la sección del LOGIN donde tengo un detalle, de algun modo la variable que declaro como $_SESSION no esta guardando el valor que se supone estoy pasando y esto me genera un "Access Denied"...
Incluyo el codigo del PHP se supone valida: "sessionauth.php"
<?php
//Start session
session_start();
//Include database connection details
require_once('cfgdb.php');
//Array to store validation errors
$MsgErr_arr = array();
//Validation error flag
$MsgErr = false;
//Connect to mysql server
$db_lnk = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$db_lnk) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);
//Input Validations
if($login == '') {
echo "Login ID missing";
$MsgErr_arr[] = 'Login ID missing';
$MsgErr = true;
}
if($password == '') {
echo "password ID missing";
$MsgErr_arr[] = 'password missing';
$MsgErr = true;
}
//If there are input validations, redirect back to the login form
if($MsgErr) {
$_SESSION['MsgErr_ARR'] = $MsgErr_arr;
session_write_close();
header("location: http://localhost:8081/appweb/php/login/loginform.php");
exit();
}
//Create query
$Qry="SELECT * FROM tblusers WHERE nameuser='$login' AND passuser='$password'";
$ExecQry=mysql_query($Qry);
//Check whether the query was successful or not
if($ExecQry) {
if(mysql_num_rows($ExecQry) == 1) {
//Login Successful
session_regenerate_id();
$usersauth = mysql_fetch_array($ExecQry);
$_SESSION['SESS_ID_USER'] = $usersauth['id_user'];
$_SESSION['SESS_NAME_USER'] = $usersauth['nameuser'];
do {
echo key($_SESSION);
echo "<br />";
echo current($_SESSION);
echo "<br />";
} while(next($_SESSION));
session_write_close();
header("location: indexauth.php");
exit();
} else {
//Login failed
header("location: authfailed.php");
exit();
}
} else {
die("Query failed");
}
?>
Cuando el usuario y la contraseña son validos se llama un 3er PHP que es donde va a comenzar a desplegarse lo que el usuario podrá acceder mediante el: "indexauth.php":
<?php
require_once('authlogon.php');
?>
<!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" />
<title>Member Index</title>
<link href="http://localhost:8081/appweb/css/loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <?php echo $_SESSION['SESS_NAME_USER']; ?></h1>
<a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a>
<p>This is a password protected area only accessible to members. </p>
</body>
</html>
Como requiero saber si el usuario sigue conectado, me auxilio del "authlogon.php"
<?php
//Start session
session_start();
//Check whether the session variable SESS_ID_USER is present or not
if(!isset($_SESSION['SESS_ID_USER']) || (trim($_SESSION['SESS_ID_USER']) == '')) {
header("location: accessdenied.php");
exit();
}
?>
Que en este ultimo es donde me doy cuenta que no guarda el valor la $_SESSION... pudieran decirme donde tengo el error.
Gracias de antemano por su apoyo, Saludos.
Oscar Carrizales.