Jump to content

Understanding Sessions


Peterod

Recommended Posts

Hi All

 

I have a basic login system on my site.  How would I go about displaying the current logged in user information from the session Id.  its quite confusing.....

<?php

require 'Mysql.php';

class Membership {

function validate_user($un, $pwd) {
	$mysql = New Mysql();
	$ensure_credentials = $mysql->verify_Username_and_Pass($un, ($pwd));

	if($ensure_credentials) {
		$_SESSION['status'] = 'authorized';
		header("location: ../selfbuild/Controlpanel.php");
	} else return "Please enter a correct username and password";

} 

function log_User_Out() {
	if(isset($_SESSION['status'])) {
		unset($_SESSION['status']);

		if(isset($_COOKIE[session_name()])) 
			setcookie(session_name(), '', time() - 1000);
			session_destroy();
	}
}

function confirm_Member() {
	session_start();
	if($_SESSION['status'] !='authorized') header("location: login.php");
}

}

 

Just need to display the user name at the moment but am kinda not getting how its done.

 

 

Do I just need

<?php
session_start(); echo $un

 

Link to comment
Share on other sites

You will likely want to store the username within the $_SESSION array.

 

function validate_user($un, $pwd) {
    $mysql = New Mysql();
    $ensure_credentials = $mysql->verify_Username_and_Pass($un, ($pwd));
    if ($ensure_credentials) {
        $_SESSION['status'] = 'authorized';
        $_SESSION['username'] = $un;
        header("location: ../selfbuild/Controlpanel.php");
    }
    return "Please enter a correct username and password";
}

 

Then, on any page....

 

session_start();
if (isset($_SESSION['username'])) {
  echo "Hello {$_SESSION['username']}";
}

Link to comment
Share on other sites

Hi,

 

After you logged in ..

 

$rows = mysql_query("select * from table_name where Username = $_SESSION['username']");

 

$row = mysql_fetch_array($rows);

 

//if you want to store all the values of the user in session u can do like that

 

$_SESSION['first_name'] = $row['first_name'];

$_SESSION['last_name'] = $row['last_name'];

$_SESSION['age'] = $row['age'];

 

//wherever you want to display the user info ucan just call like this..

 

echo $_SESSION['age'];

 

 

 

Hope you got it..

 

Thanks

 

 

 

 

Link to comment
Share on other sites

what I actually want to do is take the username from the session, which is working now, thanks, and pit it into a form so that I can get different users to complete the form and i will know which user has done so.  I will be able to figure that out now :)

 

Thanks again guys. 8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.