-1

I am learning php. I created a registration and login system. It works fine. But I'm curious about something: how can i display user details in user profile. For example name, surname, email etc. My login system IS functional but displays only email.

This is my profile.php code.

<?php 

  session_start();
if (!isset($_SESSION['email'])) {

    header('location: index2.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=UTF-8" />
<title>Untitled Document</title>
<link href="css/profile.css" rel="stylesheet" type="text/css" />


</head>

<body>
<div id="header">
<div class="header_in">
  <div class="logo"><span>EcoWebTr</span></div>
  <div class="cik"><a href="cikis.php">Çıkış</a></div>
  <!--Login informtion--></div>
</div>
<div class="container">
<div class="wrapper">
User Email:
<font color="#000066"><?php echo $_SESSION['email']; ?></font>

</div>
</div>

</body>
</html>

And also this is login.php

<?php
include("includes/connect.php");

if(isset($_POST['login'])) {

  $email = $_POST['email'];
  $password  = md5($_POST['password']);

  $check_user = "SELECT * FROM users WHERE email='$email' AND password='$password'";

  $run = mysql_query($check_user);

  if(mysql_num_rows($run)>0) {
    $_SESSION['email']=$email;


    echo"<script>window.open('profile.php','_self')</script>";

  } else {

    echo"<script>alert('wrong email or password')</script>";

  }

}
?>
m59
  • 42,346
  • 14
  • 112
  • 132
  • 2
    It's impossible to give you a full scale answer based upon the data you provided. But usually people SELECT data from the database, based upon the key value, provided as an input. – user4035 Dec 14 '13 at 14:44
  • [Read up on sessions](http://suite101.com/article/using-a-php-session-a102893) – Funk Forty Niner Dec 14 '13 at 14:56
  • As per your edited question/code `session_start();` needs to also be inside `login.php` and do read the link I gave you just above this comment. You need to assign a variable to it first. The "how to" is in that link. – Funk Forty Niner Dec 14 '13 at 15:02
  • Please do not use `mysql_*` functions anymore. They are deprecated and vulnerable to injections. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 for more informations – chill0r Dec 14 '13 at 15:57

3 Answers3

0

There are probably several different approaches you can take.

One possibility, since you said your login is working okay, and I see that you are seeing the email in to a session variable (presumably on login) is to also set session variables for other fields you would like to display.

So set first name, last name, email, etc all as session variables. Then, in your HTML, you'll be able to reference those as needed. For example, <?php echo $_SESSION['first name']; ?>

UPDATE

Given the additional info you provided, here's a short example...

if(mysql_num_rows($run)>0) {
  $_SESSION['email']=$email;

  $row = mysql_fetch_assoc($run);
  $_SESSION['firstname'] = $row['firstname'];
  $_SESSION['lastname'] = $row['lastname'];
  // etc

This example assumes that you are storing first name / last name in your database as "firstname" and "lastname". Just adjust the names as needed to match your database. This creates all the session variables... you can then access them as needed just like you accessed your email session variable.

Charlie74
  • 2,889
  • 13
  • 23
0

You'll need to query it through your database. I recommend you query it all at the time of login and save it as session variables, and then simply echo it out anywhere.

Like Charlie stated, you can echo $_SESSION['full_name']. However I usually create an associative array named User and then put info in that. For example echo $_SESSION['USER']['full_name'] sounds like a more reasonable approach.

EXAMPLE

On login success:

1) Query basic info like first name, last name, sex, birthday etc.

2) Save them in variables such as $first_name, $last_name etc.

3) Assign those variables to sessions like this:

$first_name = $_SESSION['User']['first_name'];
$birthday = $_SESSION['User']['birthday'];

On logout, simply destroy the session with session_destroy().

Eisa Adil
  • 1,673
  • 11
  • 16
-1

something like:

<div style="float:left;">User Name:</div><div style="margin-left:100px;"><?php=$_SESSION['username']?></div>
<div style="float:left;">User Surname:</div><div style="margin-left:100px;"><?php=$_SESSION['surname']?></div>

What are the other session variable names?

klarki
  • 923
  • 4
  • 12