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
https://forums.phpfreaks.com/topic/215014-understanding-sessions/
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']}";
}

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

 

 

 

 

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.