PHP Developers Network
http://forums.devnetwork.net/

question about sessions
http://forums.devnetwork.net/viewtopic.php?f=1&t=134783
Page 3 of 3

Author:  beginner123 [ Tue Mar 13, 2012 2:22 pm ]
Post subject:  Re: question about sessions

does this code have something to do with it?
Syntax: [ Download ] [ Hide ]
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:
Syntax: [ Download ] [ Hide ]
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))

are any of these lines storing username?

Author:  beginner123 [ Wed Mar 14, 2012 2:35 pm ]
Post subject:  Re: question about sessions

hello?

Author:  califdon [ Wed Mar 14, 2012 3:25 pm ]
Post subject:  Re: question about sessions


Author:  beginner123 [ Wed Mar 14, 2012 3:46 pm ]
Post subject:  Re: question about sessions

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

Author:  Celauran [ Wed Mar 14, 2012 3:48 pm ]
Post subject:  Re: question about sessions

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
Syntax: [ Download ] [ Hide ]
var_dump($_COOKIE);
var_dump($_SESSION);

after having closed and re-opened the browser.

Author:  beginner123 [ Wed Mar 14, 2012 3:52 pm ]
Post subject:  Re: question about sessions

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:
Syntax: [ Download ] [ Hide ]
<?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:
Syntax: [ Download ] [ Hide ]
<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);

Author:  Celauran [ Wed Mar 14, 2012 3:54 pm ]
Post subject:  Re: question about sessions

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.

Author:  beginner123 [ Wed Mar 14, 2012 3:59 pm ]
Post subject:  Re: question about sessions

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



Author:  Celauran [ Wed Mar 14, 2012 4:07 pm ]
Post subject:  Re: question about sessions

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.

Author:  beginner123 [ Wed Mar 14, 2012 4:38 pm ]
Post subject:  Re: question about sessions

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

Author:  califdon [ Wed Mar 14, 2012 5:51 pm ]
Post subject:  Re: question about sessions

I can't see any reason that it doesn't work, either. I don't have time today to really debug it.

Author:  deragoku [ Fri Apr 13, 2012 1:18 am ]
Post subject:  Re: question about sessions

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

Page 3 of 3 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/