How to get session to put up a variable

I started out with using $_SESSION[id] but on a special page I am trying to get the $_SESSION[username]. This page is the only time I am using that session and it worked twice all the other times it is not getting the username. do I have to start out with session username. Or does it pick up when session id is recognized by a user.

Are you placing session_start() at the beginning of any script which is using sessions?

Yes it is there… I also used the error report which let me know which line was not working.


$username = $_SESSION[username]

I am trying to get the session username to echo on a page. I have not used the session username on other pages as it was not neccessary but I need it for the forum side and for some reason all the other parts of the input to the table are going into that table but the username is not picking up. Somehow it was retrieved 3 times but now blank.

Temporarily use:


var_dump($_SESSION);

to see what is actually in your session and to make sure you spelled username correctly etc.

BTW when accessing arrays by a known key which is a string, you should quote the key:

$_SESSION[‘username’]

Otherwise you will fill up your log with notices if you have that level of error reporting turned on (which you should when in Dev mode).

Are you saying have the $_SESSION[‘username’] at the top in isset???

Oh my goodness it gives me all the $_SESSION names but not the username. How do I fix this. what happened it is in the main table. But I like that code I was wondering if this code be done but at least I see the main problem. Just need to know how to get it solved thanks for your help. Hope you can help further.

He is saying that proper way to address array members is by:
$array[‘key’]
not by
$array[key]
unquoted string in PHP usually means that you are addressing a constant.

Forgot to show the message I was so amazed.


array(4) { ["SESS_ID"]=> string(1) "2" ["SESS_FIRST_NAME"]=> string(7) "Kevin" ["SESS_LAST_NAME"]=> string(6) "Outerbridge" ["username"]=> string(0) "" }

Sorry in my real code I have it right I was just taking a short cut I know better. :slight_smile:

Well, in that case, the error would probably be where you are setting value for $_SESSION[‘username’]; Maybe a mistype?

I copied it from the table throughout all where I had it to be sure as I thought at first I mistyped it. Any idea how i can fix it

Could you send your complete code of php file that you get $_SESSION[username]? or a part of it with session_start?

this is the parse.php file which willl run when the member has filled out the section in the forum they wish to post.


php error_reporting(E_ALL);
ini_set("display_errors", 1);

session_start();
var_dump($_SESSION);

// Connect to the database
include_once "../Connection/mysql.php";

if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '') || !isset($_SESSION['username'])) { 
  '<a href="index.php">Register Account</a>
	 &nbsp;&nbsp; | &nbsp;&nbsp; 
	 <a href="login-form.php">Log In</a>';
	 
   }
if (isset($_GET['id'])) {
	 $id = $_GET['id']; // filter everything but numbers
} else if (isset($_SESSION['SESS_ID'])) {
	 $id = $_SESSION['SESS_ID'];
} else {
   '<a href="login-form.php">Log In</a>';
   
}
$sql = mysql_query("SELECT * FROM myMembers WHERE id='".$_SESSION['SESS_ID']."'");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	    echo "ERROR: You do not exist in the system";
	    exit();
}
// Be sure all form variables are present to proceed
if (!isset($_POST['post_type']) || !isset($_POST['post_body']) || !isset($_POST['fsID']) || !isset($_POST['fsTitle']) || !isset($_POST['uid']) || !isset($_POST['upass'])) {
	echo "Important variables from the form are missing.";
	exit();
}
// Filter all of the common variables
$post_type = $_POST['post_type']; 
$post_body = $_POST['post_body'];
$post_body = nl2br(htmlspecialchars($post_body));
$post_body = mysql_real_escape_string($post_body);
$forum_section_id = preg_replace('#[^0-9]#i', '', $_POST['fsID']); 
$forum_section_title = preg_replace('#[^A-Za-z 0-9]#i', '', $_POST['fsTitle']); 
$member_id = preg_replace('#[^0-9]#i', '', $_POST['uid']); 
$username =  preg_replace('#[^0-9]#i', '', $_SESSION['username']); 
$member_password = mysql_real_escape_string($_POST['upass']);
// Check the database to be sure that this forum section exists
$sql = mysql_query("SELECT * FROM forum_sections WHERE id='$forum_section_id' AND title='$forum_section_title'");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	    echo "ERROR: That forum section does not exist.";
	    exit();
}
// Prevent this member from posting more than 30 times in one day
$sql = mysql_query("SELECT id FROM forum_posts WHERE post_author_id='$member_id' AND DATE(date_time) = DATE(NOW()) LIMIT 32");
$numRows = mysql_num_rows($sql);
if ($numRows > 30) {
	echo "ERROR: You can post only 30 times per day. Your maximum has been reached.";
    exit();
}
// Add this post to the database now. The query depends on the "post_type" value
// Only if the post_type is "a" //////////////////////
if ($post_type == "a") {
	$post_title = preg_replace('#[^A-za-z0-9 ?!.,]#i', '', $_POST['post_title']);	
	if ($post_title == "") { echo "The Topic Title is missing."; exit(); }
	if (strlen($post_title) < 10) { echo "Your Topic Title is less than 10 characters."; exit(); }
	$sql = mysql_query("INSERT INTO forum_posts (username, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) 
     VALUES('".$username."','".$member_id."',now(),'a','".$forum_section_title."','".$forum_section_id."','".$post_title."','".$post_body."')") or die (mysql_error());
	$this_id = mysql_insert_id();
	//$sql = mysql_query("UPDATE forum_posts SET otid='$this_id' WHERE id='$this_id'"); 
	header("location: view_thread.php?id=$this_id"); 
    exit();
}

I am now getting this message and it is not even showing the $_SESSION username.???


array(3) { ["SESS_ID"]=> string(1) "2" ["SESS_FIRST_NAME"]=> string(7) "Michael" ["SESS_LAST_NAME"]=> string(6) "Cooke" } 
Notice: Undefined index: username 

I do not see in this code segment, where you are assigning any value to $_SESSION[‘username’].

It is in the section Filter all of the common variables


$post_type = $_POST['post_type'];  $post_body = $_POST['post_body']; $post_body = nl2br(htmlspecialchars($post_body)); $post_body = mysql_real_escape_string($post_body); $forum_section_id = preg_replace('#[^0-9]#i', '', $_POST['fsID']);  $forum_section_title = preg_replace('#[^A-Za-z 0-9]#i', '', $_POST['fsTitle']);  $member_id = preg_replace('#[^0-9]#i', '', $_POST['uid']);  $username =  preg_replace('#[^0-9]#i', '', $_SESSION['username']);  $member_password = mysql_real_escape_string($_POST['upass']);

No, there is place where $username is set:
$username = preg_replace(‘#[^0-9]#i’, ‘’, $_SESSION[‘username’]);

But at no point do I see that there is something like:
$_SESSION[‘username’] = … ;
It must be somewhere else, in some other file.

No this is the first time I am using $_SESSION[‘username’] as I am using this part as the forum to keep users private in conversation etc. All other times it is using firstname or whatever the member puts for the firstname to be viewed as they attach a pic. How can I fix this. I put the var_dump $_SESSION on all other pages but it only shows up on this. Or I am thinking should I add that to the other pages in the forum to make the connection.

Ok I put it on the page that when you are ready to post to a topic and it came up with the same problem I added !isset($_SESSION[‘username’]) to the top of the page. where before it actually got the username and you can view it in the section that it is to be in. but now I put the var_dump and !isset to the page it shows like the other page. that error message and not showing $_SESSION[‘username’]


array(3) { ["SESS_ID"]=> string(1) "2" ["SESS_FIRST_NAME"]=> string(7) "John" ["SESS_LAST_NAME"]=> string(6) "Smith" } 
Warning: Cannot modify header information - headers already sent by

This is the code I added the !isset($_SESSION[‘username’])


if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '') || !isset($_SESSION['username'])) { 
  header("location: access-denied.php");
		exit();
   }

My error message is above.