hola buenas tardes, he estado ultimamente metiendome con el tema sessiones con php y tengo una par de dudas que trás varios intentos no las pude solucionar
hice un sistemita de login para usuarios ,la cual consta de.. un index, donde podes logear o darte de alta, la cual se llama index.htm , tambien un sesion.php donde muestro la info a los uusario q se logean o sino de lo contrario le salga un error ALERT, un usuarios.php
donde tengo el codigo para dar de alta a los usuarios a una BD mysql.
index.htm
Código HTML:
Ver original<!DOCTYPE HTML>
<meta http-equiv="content-type" content="text/html" /> <meta name="author" content="www.visualsac.net" /> body{
background: black;
color:white;
font-family: trebuchet ms;
font-style:italic;
}
.regis{
color:white;
position: absolute;
right:150px;
font-family: trebuche ms;
font-size: 20px;
}
#contenedor{
color:black;
position:absolute;
top:200px;
right:500px;
left:500px;
border:5px solid;
border-radius:25px;
border-color:white;
background:#C0DDEC;
}
<script type="text/javascript">
function validar(){
if(document.formulario1.username.value.length==0){
alert('falta USUARIO')
document.formulario1.username.focus();
document.formulario1.username.style.background='#F84926'
return false;}
else{
document.formulario1.username.style.background='white'
}
if(document.formulario1.password.value.length==0){
alert('falta CONTRASEÑA')
document.formulario1.password.focus();
document.formulario1.password.style.background='#F84926'
return false;}
else{
document.formulario1.password.style.background='white'
}
document.formulario1.submit();
return false;}
<?php
session_start();
if(!empty($_SESSION['usuario'])){
echo"<table align='right' BgColor='white'>";
echo "
<td ><font color='black'> bienvenido
<b>".$_SESSION['usuario']."
</b> "."
<a href='cerrarsesion.php'>Cerrar session
</a></font></td>";
}
?>
<body onload='document.formulario1.username.focus();'>
<form name="formulario1" action="sesion.php" method='POST'>
<td>Contraseña:
</td><td><input type='password' name='password' /><br /></td> <td valign="top" ><input type="button" value="entrar" onclick="validar()"/> </td>
</table><br /><br /><center>No tenes cuenta? Registrate gratuitamente
<br /> <input type="button" value="Registrate" onclick="window.location.replace('usuarios.htm')" /></center>
sesion.php
Código HTML:
Ver originalbody{
background: black;
color:white;
font-family: trebuchet ms;
font-style:italic;
font-size: 22px;
text-align: center;
}
<?
$thank="usuarios.htm";
$host="localhost";
$root="root";
$pass="";
$base="usu";
$user=$_POST['username'];
$contra=$_POST['password'];
session_start();
$_SESSION['usuario']=$_POST['username'];
$_SESSION['clave']=$_POST['password'];
//conexión con la base de datos.
$conexion=mysql_connect($host,$root,$pass);
//Selección de la base de datos.
mysql_select_db($base,$conexion);
$query = mysql_query("SELECT * FROM usu WHERE username = '$user'") or die(mysql_error());
$datos = mysql_fetch_array($query);
if($datos['password'] ==null ) {
}else{
echo "<script> alert('Contraseña Correcta :D ');document.formulario.subtmit();
</script>";
$row = mysql_fetch_array($query);
$_SESSION["s_username"] = $row['username'];
$_SESSION["s_password"]= $row['password'];
$_SESSION["s_mail"]= $row['mail'];
$_SESSION["s_patch"]=$row['patch'];
$_SESSION["n_nombre"]=$row['nombre'];
$_SESSION["e_edad"]=$row['edad'];
echo"Bievenido
<b> ".$_SESSION['n_nombre']."
</b><a href='cerrarsesion.php'>Cerrar session
</a><br /><br />";
echo" Por ahora solo podes ver esto :D ";
echo"
<table align='center'>";
echo "
<td valign='top' colspan='3' height='10'><hr width='700'></hr></td>";
echo "
<tr><td><img width='300' height='250' src='".$_SESSION['s_patch']."'></td><br />";
echo "
<td>Usuario: ".$_SESSION['s_username']."
<br /><br />";
echo"Contraseña:".$_SESSION['s_password'] ."
<br /><br />";
echo"E-Mail :".$_SESSION['s_mail'].""."
<br /><br />";
echo"Edad :".$_SESSION['e_edad']."
</td></tr>"."
<br /><br />";
echo "
<td valign='top' colspan='3' height='10'><hr width='700'></hr></td>";
}
?>
</center>
Usuarios.php
Código HTML:
Ver original$host="localhost";
$root="root";
$pass="";
$base="usu";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
$patch = "./upload/".$_FILES["file"]["name"];
$conexion=mysql_connect($host,$root,$pass) or die (" Error en la conexion");
mysql_select_db($base,$conexion) or die (" error en la base de datos ");
$query=mysql_query("insert into usu(nombre,username,password,edad,mail,patch) values('$_POST[nom]','$_POST[usuario]','$_POST[pass]','$_POST[edad]','$_POST[mail]','$patch')",$conexion) or die ("alksdjaskldjklasjd eerror en la consulta xD ");
mysql_close($conexion);
header("location: indexu.htm");
EL PROBLEMA QUE TENGO ES EL SIGUIENTE, TODO ESTO FUNCIONA.. EL USUARIO SE PUEDE LOGEAR Y CERRAR SESION(NO PUSE EL CODIGO DE CERRAR)... LO QUE NO ME FUNCIONA ES QUE AL HACER EL NAVEGADOR HACIA ATRAS, LA SESION PIERDE EL LOGIN , ES DECIR, QUIERO QUE EL USUARIO INICIE SESION Y VEA LA PAGINA SESION.PHP Y VOLVER VOLVER HACIA ATRAS DESDE EL NAVEGADOR.. SE VEA UN BIENVENIDO Y TENGO EL NOMBRE DE LA SESION... DE HECHO LO HACE ESTE CODIGO , PERO CUANDO ME LOGEO CON CUALQUIER USUARIO Q NO ESTE REGISTRADO ME MUESTRA LA SESION IGUAL U.U..
ESPERO HAYAN ENTENDIDO UN POCO DEL CHOCLO QUE ESCRIBI :/
SALUDOS GENIOS :)