Jump to content

PHP Sessions --> Get User name?


bncplix

Recommended Posts

I have been trying everything and googling for the last hour to find this, and I can not.

 

I am using sessions in php, one file validates the login and sets the session, like so:

session_register("name");

 

Now in another file, Im using session_start(); to be able to check

I also have this:

if(!session_is_registered(name)){

 

To ensure i am online

 

One problem, How can i get the name that its session is logged in with? I just cant figure it out and I hope somebody knows!

Link to comment
Share on other sites

use:

<?php
session_start();
$_SESSION['name'] = 'my name';
echo $_SESSION['name'];
?>

 

session_register is deprecated

 

But that dosn't work accross the files (at least not for me)

 

Like, if that gets set in checklogin.php, i cant read it in index.php

Link to comment
Share on other sites

use:

session_start();
$_SESSION['name'] = 'my name';
echo $_SESSION['name'];
?>

 

session_register is deprecated

 

But that dosn't work accross the files (at least not for me)

 

Like, if that gets set in checklogin.php, i cant read it in index.php

 

You should be able to read it anywhere on your site that has session_start();  Maybe you're destroying the session somewhere and it's not retaining the name.

Link to comment
Share on other sites

use:

<?php
session_start();
$_SESSION['name'] = 'my name';
echo $_SESSION['name'];
?>

 

session_register is deprecated

 

But that dosn't work accross the files (at least not for me)

 

Like, if that gets set in checklogin.php, i cant read it in index.php

 

You should be able to read it anywhere on your site that has session_start();  Maybe you're destroying the session somewhere and it's not retaining the name.

 

I removed

session_register("myusername");

session_register("mypassword");

 

And replaced with

$_SESSION['myusername'] = $myusername;

$_SESSION['mypassword'] = $mypassword;

 

But now it dosnt even let me login, when i try it just stays at the login page and nothing else, and all the pages say I am not logged in

Link to comment
Share on other sites

Hmm, mind showing some code?

 

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
//session_register("myusername");
//session_register("mypassword");

$inTwoMonths = 60 * 60 * 24 * 1 + time(); 
setcookie('user', md5($myusername), $inTwoMonths); 

header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

You can see i have session register commented out

In my index page, I have

session_start();
echo $_SESSION['myusername']; //dosnt print anything!
if(!session_is_registered(myusername)){
echo 'You are not logged in';
}else{

 

 

Ohh and you can see i tried to setup cookies, but then that makes more problems and rather stay with only one system than having to have two constantly working, so ignore that part

Link to comment
Share on other sites

You need session_start() on every page you want to use sessions on...

 

Even the file that creates them? Ill try

 

Edit: Wow i dont know why i didnt try that in the first place, such a waste of my time googling all that time

 

Thanks guys

 

Yup. The only time you don't need it is in an included file. It should throw an error.

 

Like, if I had index.php

<?php
session_start();
include 'functions.php';
//...
echo $_SESSION['name'];
?>

I wouldn't need to call session_start in functions.php, but I will in index.php

 

However, if I didn't include functions.php and instead I went straight to that file or had my form posted there and wanted to create a session, I'd need to call session_start

 

Does that make sense?

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.