Jump to content
MakeWebGames

Access to $ir in a PHP script loaded with jQuery load()


dnenb

Recommended Posts

I have a chat in my game that uses this jquery code to load the chat messages:

<script>
setInterval(function(){
	$('#loaddiv').load('chat_loader.php');
}, 1000);
</script>

This code is present in chat.php. Why don't I have access to $ir (or any other variable available in chat.php) in chat_loader.php? This exact same way to do it works in another game of mine so I'm really confused.

Link to comment
Share on other sites

I have a chat in my game that uses this jquery code to laod the chat messages:
<script>
setInterval(function(){
       $('#loaddiv').load('chat_loader.php');
   }, 1000);
</script>

This code is present in chat.php. Why don't I have access to $ir (or any other variable available in chat.php) in chat_loader.php? This exact same way to do it works in another game of mine so I'm really confused.

If chat_loader.php doesn't have the globals.php file included you'll need to redeclare $ir via some horrible hack.

Place this code at the top of your chat_loader.php:

if (file_exists('globals_nonauth.php')) {    
   require_once('globals_nonauth.php');
} else {
   session_start();
   require_once("config.php");
   global $_CONFIG;
   define("MONO_ON", 1);
   require_once("class/class_db_{$_CONFIG['driver']}.php");
   $db = new database;
   $db->configure($_CONFIG['hostname'],
       $_CONFIG['username'],
       $_CONFIG['password'],
       $_CONFIG['database'],
       $_CONFIG['persistent']);
   $db->connect();
   $c = $db->connection_id;

   $set = array();
   $settq = $db->query("SELECT * FROM settings");
   while ($r = $db->fetch_row($settq)) {
       $set[$r['conf_name']] = $r['conf_value'];
   }
}

$userid = $_SESSION['userid'];

$is = $db->query("SELECT `u`.*, `us`.*
                    FROM `users` AS `u`
                    INNER JOIN `userstats` AS `us`
                    ON `u`.`userid`=`us`.`userid`
                    WHERE `u`.`userid` = {$userid}
                    LIMIT 1");

$ir = $db->fetch_row($is);

You might not need all the code if you've already connected to the database. This code will work for old v2 and v2.0.5b

You're probably going to want to do some checks to see if the user is logged in and exists etc.

Edited by Dave Macaulay
Link to comment
Share on other sites

Thanks! I actually thought I had access through it through the jquery load, haha :)

The ajax request is an entirely new request to the server. Completely independent of the current page that has been loaded. So you need to initialise all your classes and variables (such as $ir in this case).

You might want to optimise the $is query as I just pulled it out of core McCodes and it's rather... inefficient. I doubt you'll need all that data.

Edited code in my previous post, realised mine was including the files in the directory above!

Link to comment
Share on other sites

Yeah I just needed to get that point. I'll take what I need from the db :)

- - - Updated - - -

Hmm actually it looks like I don't have access to the session variable from there. That's odd. Do you know what could cause that?

As in, this is at the top of chat_loader.php:

 

<?php
session_start();

echo '1'.$_SESSION['userid'];

 

but it only outputs 1... The same echo will output the userid in chat.php.

Edited by dnenb
Link to comment
Share on other sites

Yeah I just needed to get that point. I'll take what I need from the db :)

- - - Updated - - -

Hmm actually it looks like I don't have access to the session variable from there. That's odd. Do you know what could cause that?

As in, this is at the top of chat_loader.php:

<?php
session_start();

echo '1'.$_SESSION['userid'];

but it only outputs 1... The same echo will output the userid in chat.php.

If you're using 2.0.5b you'll need to set the session name.

session_name('MCCSID');
@session_start();

You'll be much better of doing this:

<?php
$nohdr = true;
require_once('globals.php');

From what I can tell $nohdr stops the header being output. Didn't even know this existed!

McCodes docs anyone? @ColdBlooded

Edited by Dave Macaulay
Link to comment
Share on other sites

If you're using 2.0.5b you'll need to set the session name.

 

session_name('MCCSID');@session_start();

 

You'll be much better of doing this:

<?php
$nohdr = true;
require_once('globals.php');

 

From what I can tell $nohdr stops the header being output. Didn't even know this existed!

McCodes docs anyone? [MENTION=50433]ColdBlooded[/MENTION]

Setting the session name solved it :)

Link to comment
Share on other sites

If you're using 2.0.5b you'll need to set the session name.

session_name('MCCSID');
@session_start();

You'll be much better of doing this:

<?php
$nohdr = true;
require_once('globals.php');

From what I can tell $nohdr stops the header being output. Didn't even know this existed!

McCodes docs anyone? @ColdBlooded

Don't know dude

Link to comment
Share on other sites

I know it's not what you posted but why not load only what if required? JSON is super easy with jQuery

Do you mean passing the info to the loaded page? If so: can't do that as I have to check that the user is indeed that user in the loaded doc. So I gotta access the session.

Link to comment
Share on other sites

Do you mean passing the info to the loaded page? If so: can't do that as I have to check that the user is indeed that user in the loaded doc. So I gotta access the session.

I think you're slightly confused about what he means. Loading a $_SESSION is very quick and not something to worry about. The massive $is query on the other hand is. You can make the request even more efficient by only returning the actual data you need and processing it client side.

This looks helpful:

http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

Link to comment
Share on other sites

I think you're slightly confused about what he means. Loading a $_SESSION is very quick and not something to worry about. The massive $is query on the other hand is. You can make the request even more efficient by only returning the actual data you need and processing it client side.

This looks helpful:

http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

I thought he meant giving whatever info I needed in chat_loader.php via the load()-call as JSON or something. I use the session and a DB call to get what I need :)

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...