question about sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

does this code have something to do with it?

Code: Select all

if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
I am supposed to add code for cookies because I have no code for that

I searched for username is all the pages of my code and heres the result:
[text]Search "userName" (29 hits in 6 files)
C:\wamp\www\project\header.php (1 hits)
Line 26: echo 'Hello <b>' . ($_SESSION['userName']) . '</b>. <a class="item" href="signout.php">Sign out</a>';
C:\wamp\www\project\topic.php (2 hits)
Line 46: users.userName
Line 71: <td class="user-post">' . $posts_row['userName'] . '</td>
C:\wamp\www\project\signin.php (10 hits)
Line 19: Enter Username: <input type="text" name="userName" /><br />
Line 19: Enter Username: <input type="text" name="userName" /><br />
Line 28: if(!isset($_POST['userName']))
Line 30: $errors[] = 'The username field must not be empty.';
Line 54: userName,
Line 59: userName = '" . mysql_real_escape_string($_POST['userName']) . "'
Line 59: userName = '" . mysql_real_escape_string($_POST['userName']) . "'
Line 87: $_SESSION['userName'] = $row['userName'];
Line 87: $_SESSION['userName'] = $row['userName'];
Line 92: echo 'Welcome, ' . $_SESSION['userName'] . '. <br /><a href="index.php">Return to home page</a>.<br/>';
C:\wamp\www\project\signup.php (13 hits)
Line 11: Enter a Username: <input type="text" name="userName" /><br />
Line 11: Enter a Username: <input type="text" name="userName" /><br />
Line 22: if(isset($_POST['userName']))
Line 24: if(!ctype_alnum($_POST['userName']))
Line 26: $errors[] = 'The username can only contain letters and digits.';
Line 28: elseif(strlen($_POST['userName']) > 45)
Line 30: $errors[] = 'The username cannot be longer than 45 characters.';
Line 34: $sql = mysql_query("SELECT userID FROM users WHERE userName='".$_POST['userName']."' LIMIT 1");
Line 34: $sql = mysql_query("SELECT userID FROM users WHERE userName='".$_POST['userName']."' LIMIT 1");
Line 38: $errors[] = 'The username already exists please choose another.';
Line 45: $errors[] = 'The username field must not be empty.';
Line 87: users(userName, userPassword, userEmailAddress ,userDate, userLevel)
Line 88: VALUES('" . mysql_real_escape_string($_POST['userName']) . "',
C:\wamp\www\project\signout.php (1 hits)
Line 41: $_SESSION['userName'] = NULL;
C:\wamp\www\project\connect.php (2 hits)
Line 6: $username = "root";
Line 11: if(!mysql_connect($host, $username, $password))[/text]
are any of these lines storing username?
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

hello?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about sessions

Post by califdon »

are any of these lines storing username?
Well, anyplace that says $userName = something is storing something in that variable.

But that's not really going to help you, since there's only one script running at a time and unless it's the script that is causing the problem, it doesn't matter. Start with telling us, step by step, what you are doing and what scripts are being run (in other words, what is the URL that is showing in the browser address bar at the time the problem occurs). Something like this:

User logs in from login page, signin.php.
User logs out from logout page, signout.php.
User closes the browser.
User opens the browser again.
User returns to main page, header.php.
User's name appears in header.php, even though he has logged out.

This is the only way we can possibly get any idea of what's going on. If we can isolate it to the script, we may then ask you to post that entire script, but if you do, please remember to use the

Code: Select all

 button in our Post A Reply form, to make it easier for us to read.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

ok so user signs in from sign in page, signin.php
user returns to home page, index.php and name appears in userbar, header.php (i #included the header file to all pages)
user closes brower
user opens brower again
user returns to home page, index.php
user's name still in userbar
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: question about sessions

Post by Celauran »

So after having closed the browser, the user is not required to sign in again? Is that right? If so, please post the complete index.php and header.php code. Please also post the output of the following

Code: Select all

var_dump($_COOKIE);
var_dump($_SESSION);
after having closed and re-opened the browser.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

ok the user is still sign in when i reopen the brower so the name is still in the userbar
heres is the index page:

Code: Select all

<?php
//index.php
include 'connect.php';
include 'header.php';

$sql = "SELECT
			categories.categoryID,
			categories.categoryName,
			categories.categoryDescription,
			categories.sticky,
			COUNT(topics.topicID) AS topics
		FROM
			categories
		LEFT JOIN
			topics
		ON
			topics.topicID = categories.categoryID
		GROUP BY
			categories.categoryName, categories.categoryDescription, categories.categoryID
		ORDER BY
			sticky
			DESC";
		

$result = mysql_query($sql);

if(!$result)
{
	echo 'The categories could not be displayed, please try again later.';
}

else
{
	if(mysql_num_rows($result) == 0)
	{
		//no categories have been made yet
		echo 'No categories defined yet.';
	}
	else
	{
		//make the table
		echo '<table border="1">
			  <tr>
				<th>Category</th>
				<th>Last topic</th>
			  </tr>';	
			
		while($row = mysql_fetch_assoc($result))
		{				
			echo '<tr>';
				echo '<td class="leftpart">';
					echo '<h3><a href="category.php?id=' . $row['categoryID'] . '">' . $row['categoryName'] . '</a></h3>' . $row['categoryDescription'];
				echo '</td>';
				echo '<td class="rightpart">';
				
				//fetch last topic for each category
					$topicsql = "SELECT
									topicID,
									topicSubject,
									topicDate,
									topicCategory
								FROM
									topics
								WHERE
									topicCategory = " . $row['categoryID'] . "
								ORDER BY
									topicDate
								DESC
								LIMIT
									1";
								
					$topicsresult = mysql_query($topicsql);
				
					if(!$topicsresult)
					{
					//error
						echo 'Last topic could not be displayed.';
					}
					else
					{
						if(mysql_num_rows($topicsresult) == 0)
						{
							echo 'no topics';
						}
						else
						{
							while($topicrow = mysql_fetch_assoc($topicsresult))
							echo '<a href="topic.php?id=' . $topicrow['topicID'] . '">' . $topicrow['topicSubject'] . '</a> at ' . date('d/m/Y', strtotime($topicrow['topicDate']));
						}
					}
				echo '</td>';
			echo '</tr>';
		}
	}
}

include 'footer.php';
?>
and here is the header page:

Code: Select all

<html>
<head>
 	<title>PHP-MySQL forum</title>
	<link rel="stylesheet" href="style.css" type="text/css"> 
</head>
<body>
<h1>Forum</h1>
	<div id="wrapper">
	<div id="menu">
		<a class="item" href="/project/index.php">Home</a> -
		<a class="item" href="/project/create_topic.php">Create a topic</a> -
		
		<?php 
		if(isset($_SESSION['signed_in']) == true && $_SESSION['userLevel'] == 1)
		{ 
			echo "<a class=\"item\" href=\"/project/create_cat.php\">Create a category</a> -
			<a class=\"item\" href=\"/project/delete_cat.php\">Delete a category</a> -
			<a class=\"item\" href=\"/project/delete_topic.php\">Delete a topic</a>"; 
		}
		?> 
		
		<div id="userbar">
		<?php
		if(isset($_SESSION['signed_in']) == true)
		{
			echo 'Hello <b>' . ($_SESSION['userName']) . '</b>. <a class="item" href="signout.php">Sign out</a>';
		}
		else
		{
			echo '<a class="item" href="signin.php">Sign in</a> or <a class="item" href="signup.php">create an account</a>';
		}
		?>
		</div>
		</div>
		<div id="content">
sorry don't know what you mean with show the output for var_dump($_COOKIE); and var_dump($_SESSION);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: question about sessions

Post by Celauran »

Add that code to the top of header.php. It will produce output onscreen. Copy/paste that output here, please.

At a glance, I don't see anything unusual about either of the files you've posted and it still isn't clear to me why the user isn't being signed out.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

do you mean copy it on the top of my index.php page because if i put it in header.php it just prints var_dump($_COOKIE);var_dump($_SESSION);

when I put it into index.php it says
array
'PHPSESSID' => string 'gp372hfr0arechepo65aq5tc90' (length=26)

and then the message
( ! ) Notice: Undefined variable: _SESSION in C:\wamp\www\project\index.php on line 3

null


User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: question about sessions

Post by Celauran »

There's no reason it shouldn't work in header.php. You did enclose it in <?php ?> tags, right?

Worst case, post the entire script here, along with a database dump, and I'll set it up locally and take a look.
beginner123
Forum Commoner
Posts: 70
Joined: Fri Feb 24, 2012 9:56 am

Re: question about sessions

Post by beginner123 »

yes all php tags are closed. I'll will take another close look at all the code and if I still can't find the problem I'll post all the code here
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about sessions

Post by califdon »

I can't see any reason that it doesn't work, either. I don't have time today to really debug it.
deragoku
Forum Newbie
Posts: 3
Joined: Thu Apr 12, 2012 2:28 am

Re: question about sessions

Post by deragoku »

It tell you that indeed the username session variable is not defined, so it was destroyed.
Post Reply