Jump to content

PHP Sessions and Require within iFrames


jdeutsch

Recommended Posts

I'm attempted to put together a simple chat script based solely on PHP/MySQL.  I'm using iframes to juggle the chat window, and the list of users currently online, however, I keep running into problems.  I've worked with iframes and php together before and never experienced these issues, so I'm stumped.

 

I've got most of the site-wide functions grouped into a single file, for easy access, and have been requiring this file on each page where the functions will be used.  This also includes my MySQL connection script.

 

Using session_start and require on the parent page works fine, however, if I attempt to use either on the iframe source pages, I get a "session already started" error, and a "cannot redeclare XXXX function" error respectively.  However, if I comment out the session_start or the require, the iframe source no longer recognizes the session variables, or the previously declared functions.  I don't know what I'm doing wrong, and I'm sure it's something remarkably simple, but I've been going nuts trying to solve this for several hours now.

 

Below are the affected snippets:

INDEX.PHP (the parent page for the chat frames)

<?
session_start();
require("../scripts/includes.php");
if(!isset($_SESSION['chat_id'])) $_SESSION['chat_id']=generate_chat_id();
?>
...
<iframe src="chat_window.php" scrolling="auto" name="chat_window" id="chat_window"></iframe>
   <iframe src="online.php" scrolling="auto" name="online" id="online"></iframe>
   <div id="controls">
    <form method="post" action="chat_window.php" target="chat_window">
 <textarea name="msg" id="chat_msg"></textarea>
 <input type="hidden" name="chat_id" value="<? print $_SESSION['chat_id']; ?>" />
 <input type="submit" value="Submit" name="send" id="submit_button" />
</form>
   </div>

 

CHAT_WINDOW.PHP (displays the chat dialogue)

<?
session_start();
require("../scripts/includes.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#FFFFFF">
<?
$chat=get_chat($_SESSION['chat_id']);
for($i=0;$i<count($chat);$i++) {
if($chat[$i]['uid']==$_SESSION['uid']) $other_style=" blue";
else $other_style="red";
if(isset($_SESSION['admin'])) $a="admin";
else $a="";
print "<span class=\"dialogue\"><span class=\"username".$other_style."\">".get_username_from_id($chat[$i]['uid'],$a)." <span class=\"small\">(".date("m/d/Y h:i:sA",$chat[$i]['timestamp']).")</span> <span class=\"text\">".$chat[$i]['dialogue']."</span></span>";
}
print time();
?>
</body>
</html>

 

ONLINE.PHP (displays the users online)

<?
session_start();
require("../scripts/includes.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#FFFFFF">
<?
$online=get_users_online();
for($i=0;$i<count($online);$i++) {
print "<span class=\"online\"><a href=\"?u=".$online[$i]."&invite&new_chat\">".$online[$i]."</a></span>";
}
?>
</body>
</html>

 

INCLUDES.PHP (running list of all required scripts)

<?
require("mysql.php");
require("functions.php");
require("online.php");
?>

 

Any and all help with this would be appreciated.

 

Happy holidays!

Link to comment
https://forums.phpfreaks.com/topic/83118-php-sessions-and-require-within-iframes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.