How to get the data of a logged-in user to make his profile?

2

I have only that problem, how can I make it for the user to grasp.

This is what I have in login.php

<form method="POST" action ="revisar.php" target="_top"/>
<table align="center">
 <tr>
      <td>
        Usuario
      </td>
      <td>
        <input type="name" name="Usuarios"/>
      </td>
 </tr>
 <tr>
      <td>
        Contraseña
      </td>
      <td>
        <input type="password" name="Contrasena"/>
      </td>
 </tr>

 

This is what I have in revising.php:

<?php
session_start();
$Usuarios = $_POST['Usuarios'];
$Contrasena = $_POST['Contrasena'];
$Usuarios = stripcslashes($Usuarios);
$Contrasena = stripcslashes($Contrasena);
$Usuarios = mysql_real_escape_string($Usuarios);
$Contrasena = mysql_real_escape_string($Contrasena);

mysql_connect("localhost","root","");
mysql_select_db("usuarios");

$Contrasena = md5($Contrasena);
$result = mysql_query("select * from registro where Usuarios = '$Usuarios'") 
or die(" ERROR ".mysql_error());
$row = mysql_fetch_array($result);
$res =  "Error en usuario y/o contraseña!";
if($row['Usuarios']){ 
if($row['Contrasena'] == $Contrasena){
    $res = '';
    $_SESSION['Cargo']=$row['Cargo'];
    $_SESSION['id']=$row['id'];
    header("Location: index.php");
}
}
echo $res;
?>

How could the profile be made with this code?

    
asked by Shredder 16.07.2017 в 16:53
source

2 answers

0

Basically when you retrieve the data from the database, save the data in the variables of $ _SESSION.

For example:

if($row['Contrasena'] == $Contrasena){
    $res = '';
    $_SESSION['Cargo']=$row['Cargo'];
    $_SESSION['id']=$row['id'];
    header("Location: index.php");
}

In that part of the code, where you have declared the $ _SESSION variables for the Position and the ID, you also create variables for the personal data:

$_SESSION['Nombre'] = $row['NombreEnLaBD'];
$_SESSION['Apellidos'] = $row['ApellidosenlaBD'];
$_SESSION['Genero'] = $row['GeneroenlaBD'];

And everything you need to keep. Then, on your profile page, you just have to do the echo of the variable $ _SESSION and that's it.

echo $_SESSION['Nombre'];
    
answered by 16.07.2017 в 18:05
0

You can create a cookie and access it from any page in your domain. The cookie will remain active for as long as you determine, so that the user does not have to take it back every time he enters.

if($row['Usuarios'] && $row['Contrasena'] == $Contrasena){
        $res = '';
        $Cargo=$row['Cargo'];
        $id=$row['id'];

        //Cookie que dura un año
        setcookie('miwebcookie',$cargo.':'.$id,time()+3600*24*365);
        // Si quieres que la cookie expire al acabar la sesión no añadas el último término, que es el tiempo de duración

        header("Location: index.php");
}

And in index.php you check that the cookie exists

if (isset($_COOKIE['miwebcookie']) {
 //Hay cookie de usuario hago lo que corresponda
 // aquí accedo a los valores almacenados en la cookie (segundo parámetro)
 $cookie_valor = $_COOKIE['miwebcookie']; 
} else { // no hay cookie le llevo a registro o lo que corresponda
}
    
answered by 16.07.2017 в 19:19