Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/12/2012, 10:37
nickou
 
Fecha de Ingreso: diciembre-2012
Mensajes: 53
Antigüedad: 11 años, 4 meses
Puntos: 0
Problemas de sessiones

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
  1. <!DOCTYPE HTML>
  2.     <meta http-equiv="content-type" content="text/html" />
  3.     <meta name="author" content="www.visualsac.net" />
  4.     <style type="text/css">
  5. body{
  6.     background: black;
  7.     color:white;
  8.     font-family: trebuchet ms;
  9.     font-style:italic;
  10.    
  11. }
  12.     .regis{
  13.         color:white;
  14.         position: absolute;
  15.         right:150px;
  16.         font-family: trebuche ms;
  17.         font-size: 20px;
  18.     }
  19.    
  20.     #contenedor{
  21.         color:black;
  22.         position:absolute;
  23.         top:200px;
  24.         right:500px;
  25.         left:500px;
  26.         border:5px solid;  
  27.         border-radius:25px;
  28.         border-color:white;
  29.         background:#C0DDEC;
  30.        
  31.        
  32.        
  33.     }
  34.  
  35.  
  36. <script type="text/javascript">
  37.  
  38. function validar(){
  39.             if(document.formulario1.username.value.length==0){
  40.                 alert('falta USUARIO')
  41.                 document.formulario1.username.focus();
  42.                 document.formulario1.username.style.background='#F84926'
  43.             return false;}
  44.             else{
  45.                 document.formulario1.username.style.background='white'
  46.             }
  47.                 if(document.formulario1.password.value.length==0){
  48.                     alert('falta CONTRASEÑA')
  49.                 document.formulario1.password.focus();
  50.                 document.formulario1.password.style.background='#F84926'
  51.                 return false;}
  52.                 else{
  53.                 document.formulario1.password.style.background='white'
  54.             }
  55.            
  56.             document.formulario1.submit();
  57.                 return false;}
  58.     <title>Untitled 22</title>
  59. </head>
  60.  
  61. <?php
  62.  
  63.  
  64.  
  65.  
  66.  
  67. session_start();
  68.  
  69.  
  70.  
  71. if(!empty($_SESSION['usuario'])){
  72.    
  73.        echo"<table  align='right' BgColor='white'>";
  74.         echo "<td ><font color='black'> bienvenido <b>".$_SESSION['usuario']."</b> "."<a href='cerrarsesion.php'>Cerrar session </a></font></td>";
  75.         echo"</table>";
  76. }
  77.  
  78. ?>
  79. <body onload='document.formulario1.username.focus();'>
  80.  
  81.  
  82. <br /><br /><br />
  83. <div id="contenedor">
  84. <br />
  85.  <form name="formulario1" action="sesion.php" method='POST'>
  86.  <table align="center">
  87.  <tr>
  88.  
  89.  
  90.  
  91. <td>Usuario: </td><td><input type='text' name='username'/><br /></td>
  92. </tr>
  93. <tr>
  94. <td>Contraseña: </td><td><input type='password' name='password' /><br /></td>
  95. </tr>
  96. <tr>
  97. <td valign="top"  ><input type="button" value="entrar"  onclick="validar()"/> </td>
  98. </tr>
  99.  
  100. </tr>
  101.  
  102. </table><br /><br /><center>No tenes cuenta? Registrate gratuitamente<br /> <input type="button" value="Registrate" onclick="window.location.replace('usuarios.htm')" /></center>
  103.  </form>
  104. <br />
  105. </div>
  106.  
  107.  
  108.  
  109.  
  110. </body>
  111. </html>
sesion.php

Código HTML:
Ver original
  1. <style type="text/css">
  2. body{
  3.     background: black;
  4.     color:white;
  5.     font-family: trebuchet ms;
  6.     font-style:italic;
  7.     font-size: 22px;
  8.     text-align: center;
  9. }
  10.  
  11.  
  12.  
  13. </head>
  14.  
  15.  
  16. <?
  17.  
  18.  
  19.  
  20. $thank="usuarios.htm";
  21.  
  22. $host="localhost";
  23. $root="root";
  24. $pass="";
  25. $base="usu";
  26. $user=$_POST['username'];
  27. $contra=$_POST['password'];
  28.  
  29.  
  30. session_start();
  31.  
  32. $_SESSION['usuario']=$_POST['username'];
  33. $_SESSION['clave']=$_POST['password'];
  34.  
  35. //conexión con la base de datos.
  36. $conexion=mysql_connect($host,$root,$pass);
  37. //Selección de la base de datos.
  38. mysql_select_db($base,$conexion);
  39. $query = mysql_query("SELECT * FROM usu WHERE username = '$user'") or die(mysql_error());
  40.  
  41.  
  42. $datos = mysql_fetch_array($query);
  43. if($datos['password'] ==null ) {
  44.    
  45.    
  46.  
  47.  
  48.    
  49.    }else{
  50.    echo "<script> alert('Contraseña Correcta :D ');document.formulario.subtmit(); </script>";
  51.  
  52. $row = mysql_fetch_array($query);
  53.  
  54. $_SESSION["s_username"] = $row['username'];
  55. $_SESSION["s_password"]= $row['password'];
  56. $_SESSION["s_mail"]= $row['mail'];
  57. $_SESSION["s_patch"]=$row['patch'];
  58. $_SESSION["n_nombre"]=$row['nombre'];
  59. $_SESSION["e_edad"]=$row['edad'];
  60.    
  61.     echo"Bievenido<b>  ".$_SESSION['n_nombre']." </b><a href='cerrarsesion.php'>Cerrar session </a><br /><br />";
  62.     echo" Por ahora solo podes ver esto :D ";
  63.  
  64. echo"<table align='center'>";
  65. echo "<td valign='top' colspan='3' height='10'><hr width='700'></hr></td>";
  66.  echo "<tr><td><img width='300' height='250' src='".$_SESSION['s_patch']."'></td><br />";
  67. echo "<td>Usuario: ".$_SESSION['s_username']."<br /><br />";
  68. echo"Contraseña:".$_SESSION['s_password'] ."<br /><br />";
  69. echo"E-Mail :".$_SESSION['s_mail'].""."<br /><br />";
  70. echo"Edad :".$_SESSION['e_edad']."</td></tr>"."<br /><br />";
  71.  
  72.  echo "<td valign='top' colspan='3' height='10'><hr width='700'></hr></td>";
  73.  
  74. }
  75.  
  76.  
  77. echo"</table>";
  78.  
  79. ?>
  80. </body>
  81. <center><br />
  82.  
  83. </center>
  84. </html>






Usuarios.php

Código HTML:
Ver original
  1. $host="localhost";
  2. $root="root";
  3. $pass="";
  4. $base="usu";
  5.  
  6.  
  7.  
  8.  
  9. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
  10.  
  11. $patch = "./upload/".$_FILES["file"]["name"];
  12.  
  13. $conexion=mysql_connect($host,$root,$pass) or die (" Error en la conexion");
  14.  
  15. mysql_select_db($base,$conexion) or die (" error en la base de datos ");
  16.  
  17. $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 ");
  18.  
  19. mysql_close($conexion);
  20.  
  21. 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 :)