Zubrag.com
May 22, 2012, 09:54:18 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Retrieve current logged user  (Read 706 times)
kino75
Newbie
*
Posts: 2


« on: February 23, 2011, 06:50:23 am »

Hi! I'm fairly new to php.
I've assembled a password-protected page  (your script rocks!) with a form and successfully connected it to a mysql database.
I'm wondering if it's possible to retrieve the current logged in username ('access_login' variable, is it?) and store this info along with those typed in the form in my database.
How should I set this up?

I've tried this but doesn't seem to work: the other data got inserted in the database but login was not.

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

   //connection parameters
   
   $login= $_POST['access_login'];
   $surname= $_POST['surname'];
   $name= $_POST['name'];
   $sqlinsert = "INSERT INTO mytable (login, surname, name) VALUES ('$login', '$surname', '$name')";

Thanks in advance!

Logged
kino75
Newbie
*
Posts: 2


« Reply #1 on: February 23, 2011, 06:04:50 pm »

So, it seems I got it to work!
I started from this topic http://www.zubrag.com/forum/index.php/topic,476.0.html and I've made some changes. I hope this can be useful to someone else.

1. Insert this lines at the top of the to-be-protected document, after the include statement (modify red text to fit your needs):

<?php
   session_start();
   if ($_SESSION['username'] == "") {
   $_SESSION['username'] = $login;
   }
   
   $yourvariable= $_SESSION['username'];
?>

This is to set a variable that won't be resetted after reloading the page and that can be used across the document with echo or <?=.... ?>.

2. Modify password_protect.php adding some lines as follow (in red):

// logout?
if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/'); // clear password;
  header('Location: ' . LOGOUT_URL);
 session_start();
  $_SESSION = array();
  session_destroy();

  exit();
  }

This is to unset the variable and to reset the session so you can logout and login again with a different username and have it stored as a new value for $yourvariable without the need of closing and reopening your browser.

I tested it with Firefox, Internet Explorer and Chrome and for me is working perfectly.
I'm just a newbie to php so I'm not sure if this can create problems with other elements.......

Hope it helps!!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC