PHP, cookies & YaBB [Solved]

  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

I'm writing a chat page for my forum (uses YaBB v2.2.1)

I'm reading the cookies with
Code: [ Select ]
$username = $_REQUEST['Y2User-59736'];


This usually works ok, but after the user has submitted a single message (html form submits text to php file "post.php", puts text into mysql table, header redirects to html page), the above code no longer works for retrieving the cookie.

Logging out and then in on the forum sometimes fixes the problem.

Am I doing it the right way? Should I be using sessions?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

How about $_COOKIE['Y2User-59736'];?

Also, why don't you use SESSIONs?
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

How do I do that? :)

I'm new to the whole php/sessions thing.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Well, for sessions, all you have to do is put session_start(); right after you put <?php at the VERY TOP OF THE PHP PAGE!!!

And then you set the session like $_SESSION['key'] = $value; (Yes... just like an associative array), and you would echo it as... echo $_SESSION['key'] -OR0 print_r($_SESSION) (<-- Not recommended).
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

I'm having a few troubles with it.

When a user first goes to my chatroom, it recognises their username from the forum. Then after a post, it loses the information & recognises them as guest (cookie doesn't read?).

Would there already be a session active?

Can I find out what 'key' the forum has used used? Is there a way to read the active sessions?


I've got currently:

Code: [ Select ]
if (isset($_SESSION['user_realname']))
{
    //load variables from session
    $user_realname = $_SESSION['user_realname'];
    $user_position = $_SESSION['user_position'];
}
else
{
    #retrieve cookie
    $username = $_COOKIE['Y2User-59736'];
   
    #if no cookie, username = guest
    ...
 
    #else retrieve realname from username
    ...
 
    if(!isset($_SESSION)){      //if no session active
    session_start();        //start a new session
    //store variables to session
    $_SESSION['user_realname']=$user_realname; 
    $_SESSION['user_position']=$user_position;
    }
}
  1. if (isset($_SESSION['user_realname']))
  2. {
  3.     //load variables from session
  4.     $user_realname = $_SESSION['user_realname'];
  5.     $user_position = $_SESSION['user_position'];
  6. }
  7. else
  8. {
  9.     #retrieve cookie
  10.     $username = $_COOKIE['Y2User-59736'];
  11.    
  12.     #if no cookie, username = guest
  13.     ...
  14.  
  15.     #else retrieve realname from username
  16.     ...
  17.  
  18.     if(!isset($_SESSION)){      //if no session active
  19.     session_start();        //start a new session
  20.     //store variables to session
  21.     $_SESSION['user_realname']=$user_realname; 
  22.     $_SESSION['user_position']=$user_position;
  23.     }
  24. }


But it's still not working :(
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

It doesn't seem to ever create a session. :S

I think the main problem is that the cookie isn't getting loaded (it exists on my computer, it just isn't being read)
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

It doesn't matter whether I use $_COOKIE or $_REQUEST - same result. Works directly after navigating from the forum, but not after the first refresh.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

Scrap all that, it was a different problem. All that code/time/effort/energy wasted. But I got the answer.

This site helped: http://www.webmasterworld.com/php/3390932.htm

First the user was getting directed to http://domain.com, then after one message they'd be taken to post.php, and back to http://www.domain.com.

The cookies were saved without www at the front, and the http://www.domain.com site couldn't find them.

:D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Well, glad that it got solved.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 48 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.