Link to home
Start Free TrialLog in
Avatar of JCGreyling
JCGreyling

asked on

PHP sessions

HI

I have a problem with my sessions. I'm writing a chatroom. After I logged in(log_inn.php) it goes to Chat_test.php that loads a Iframe(rows.php) and when you submit the text, it runs chat_sub.php. After I log in, I print the $_SESSION as the usename "Welcome $_SESSION" , but after the first time I submit my form, I loos my session info.

Please help.

Log_inn.php
<?php
session_start();
      include'conn.php';
      include'vars.php';
?>
<table width="100%" border="0" cellpadding="4">
<?php
if(!isset($msg)){
$memdesp = "";
}if(!isset($mem_dsp)){
$mem_dsp = "";
}
$sql = "SELECT * FROM message LIMIT 10";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)){
      $msg = $row[0];
$memdsp = "<tr>                                                
<td WIDTH=400 colspan=2 align=left valign=top><p align=left> $row[0]</p></td>
</tr>";
            if (!isset($mem_dsp)){
                    $mem_dsp = '';
                    }
            $mem_dsp = $mem_dsp.$memdsp;
}
mysql_free_result($result);
print $mem_dsp;
?>
</table>

Chat_test.php
<?php
session_start();
//$_SESSION['Username'] = $Username;
      include'conn.php';
      include'vars.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.1 transitional//EN">
<HTML>
<HEAD>
<TITLE>Waterkloof Aikido</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<LINK href="pro.css" rel=stylesheet>
</HEAD>
<BODY class=background leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0" bottommargin="0">
      <table height="500" align="left" cellSpacing=0 cellPadding=0 width=500 border=0>
            <tr>
                  <td align="center" class=Tbox3 vAlign=top bgColor=#2a2a2a>                                                
                        <table align="center" cellSpacing=1 cellPadding=10 width="499" border=0>
                              <tr>
                                    <td valign="top" align=left>
                          <form action="chat_sub.php" method="post" name="chat_sub">
                                      <input  type="hidden" name="writer" value="<?php print $_SESSION['Username'] ?>">
<table width="100%" border="1" bgcolor="#2A2A2A" align="center" cellpadding="2" cellspacing="2">
  <tr>
    <td colspan="2" ><?php print "Welcome " . $_SESSION['Username']; ?></td>
  </tr>
  <tr>
        <TD width=325>
      <IFRAME name="text_disp" id="datamain" src="rows.php"  marginWidth="0" marginHeight="0"  frameBorder="0" width="500" height="400"><br>
      <br>
        
      </IFRAME></TD>
  </tr>  
  <tr>
    <td height="131" align="center" valign="bottom">
      <select name="col">
<option>Black</option>
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>Orange</option>
</select>
<input type="button" name="sub1" value=":)" onClick="doChange()">
<input type="button" name="sub2" value=":(">
<input type="button" name="sub3" value=":D">
<br>      <br>      
      <textarea name="text_input" cols="50" rows="5"></textarea><br>
      <input name="text_sub" type="submit" value="SUBMIT">&nbsp;<input name="reset" type="reset" value="RESET">      </td>      
  </tr>
</table>
</form></td>
                              </tr>
                        </table>
            </td>
                                          <td  bgColor=#000000>&nbsp;                                          </td>
            <td>
                  
      <tr>
          <td align="left" valign="top">
                  
            </td>
      </tr>
</table>
            </td>
      </tr>
</table>
</BODY>
</HTML>

chat_sub.php
<?php
session_start();
      include'conn.php';
      include'vars.php';
$user = "<strong>$_SESSION[Username] :</strong>";
$writer = $_POST['writer'];
$time = time();
//create the SQL statement for inserting text message into the database
       $sql = "INSERT INTO message VALUES ('$user $text_input', '$writer', $time)";
      $result = mysql_query($sql);
//execute the SQL statement
       if ($result){
       include'chat_test.php';
             } else {
               print 'something went wrong';
            }
?>

rows.php
<?php
session_start();
      include'conn.php';
      include'vars.php';
?>
<table width="100%" border="0" cellpadding="4">
<?php
if(!isset($msg)){
$memdesp = "";
}if(!isset($mem_dsp)){
$mem_dsp = "";
}
$sql = "SELECT * FROM message LIMIT 10";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)){
      $msg = $row[0];
$memdsp = "<tr>                                                
<td WIDTH=400 colspan=2 align=left valign=top><p align=left> $row[0]</p></td>
</tr>";
            if (!isset($mem_dsp)){
                    $mem_dsp = '';
                    }
            $mem_dsp = $mem_dsp.$memdsp;
}
mysql_free_result($result);
print $mem_dsp;
?>
</table>
ASKER CERTIFIED SOLUTION
Avatar of alextr2003fr
alextr2003fr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JCGreyling
JCGreyling

ASKER

I do assign a value t:
$Username = $_POST['Username'];....

$_SESSION['Username'] = $Username;

It's the name the user fils in in te login form.

Explain  include_once

Thanx
php.net : http://fr3.php.net/include_once :
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

check if you are not re-setting your session values in
conn.php and vars.php
Here's my vars.php

<?php
$name = ''; // Default value
if(isset($_POST['name'])){
$name = $_POST['name'];
}
$email = '';
if(isset($_POST['email'])){
$email = $_POST['email'];
}
$username = '';
if(isset($_POST['username'])){
$username = $_POST['username'];
}
$pass = '';
if(isset($_POST['pass'])){
$pass = $_POST['pass'];
}
$Username = '';
if(isset($_POST['Username'])){
$Username = $_POST['Username'];
}
$Password = '';
if(isset($_POST['Password'])){
$Password = $_POST['Password'];
}
$text_input = '';
if(isset($_POST['text_input'])){
$text_input = $_POST['text_input'];
}
##########################################

?>

and conn.php

<?php
$dbcon = mysql_connect("192.168.41.16", "webmaster", "lobo1004");
if (!$dbcon){
echo 'Unable to connect';
echo mysql_error();
}
$mm = mysql_select_db("aikido", $dbcon);
if (!$mm){
echo 'Unable to connect to Chat Room Database';
}
?>






Better would be:
$_SESSION['Username'] = $_POST['Username'];.

include_once:
http://uk.php.net/manual/en/function.include-once.php

" The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once."
How do I use javascript in this

<?php
session_start();
//$_SESSION['Username'] = $Username;
      include'conn.php';
      include'vars.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.1 transitional//EN">
<HTML>
<HEAD>
<TITLE>Waterkloof Aikido</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<LINK href="pro.css" rel=stylesheet>
</HEAD>
<BODY class=background leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0" bottommargin="0">
      <table height="500" align="left" cellSpacing=0 cellPadding=0 width=500 border=0>
            <tr>
                  <td align="center" class=Tbox3 vAlign=top bgColor=#2a2a2a>                                                
                        <table align="center" cellSpacing=1 cellPadding=10 width="499" border=0>
                              <tr>
                                    <td valign="top" align=left>
                          <form action="chat_sub.php" method="post" name="chat_sub">
                                      <input  type="hidden" name="writer" value="<?php print $_SESSION['Username'] ?>">
<table width="100%" border="1" bgcolor="#2A2A2A" align="center" cellpadding="2" cellspacing="2">
  <tr>
    <td colspan="2" ><?php print "Welcome " . $_SESSION['Username']; ?></td>
  </tr>
  <tr>
        <TD width=325>
      <IFRAME name="text_disp" id="datamain" src="rows.php"  marginWidth="0" marginHeight="0"  frameBorder="0" width="500" height="400"><br>
      <br>
        
      </IFRAME></TD>
  </tr>  
  <tr>
    <td height="131" align="center" valign="bottom">
      <select name="col">
<option>Black</option>
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>Orange</option>
</select>
<input type="button" name="sub1" value=":)" onClick="doChange()">
<input type="button" name="sub2" value=":(">
<input type="button" name="sub3" value=":D">
<br>      <br>      
      <textarea name="text_input" cols="50" rows="5"></textarea><br>
      <input name="text_sub" type="submit" value="SUBMIT">&nbsp;<input name="reset" type="reset" value="RESET">      </td>      
  </tr>
</table>
</form></td>
                              </tr>
                        </table>
            </td>
                                          <td  bgColor=#000000>&nbsp;                                          </td>
            <td>
                  
      <tr>
          <td align="left" valign="top">
                  
            </td>
      </tr>
</table>
            </td>
      </tr>
</table>
</BODY>
</HTML>
  to add the smily ... I have no idea how to pass the value of a fom buttom .
still do not see where your populate your session values? for example for the name it should be :
Chat_test.php
<?php
session_start();
     include 'conn.php';
     include 'vars.php';
     $_SESSION['Username'] = $Username;
?>
NB: uncomment your line //$_SESSION['Username'] = $Username; and put it after the includes
the variable $Username should be global, but if your doubting, try to do :
Chat_test.php
<?php
session_start();
     include 'conn.php';
     include 'vars.php';
     global $Username;
     $_SESSION['Username'] = $Username;
?
>
Any help with the fom buttons and text color? How will I tell the form to add a button value and change the text color?

Thanx
to add smileys use php (no need for java to do this) preg_replace
exemple :   $text = preg_replace("/:\)/i",$img_happy,$text);
Please explain how I will use $text = preg_replace("/:\)/i",$img_happy,$text); with  chat_test.php?
same for text/button color use CSS in combination with switches/if statements and regular expressions if you need to replace something in the code
you put your html code in a variable, for example you call it text :
then you replace all ":)" ";)" etc etc in it by your smileys
ex:
 
  $text = 'hello :)';
  $img_happy = '<img src="smilies/happy.gif" alt="" >';
  $text = preg_replace("/:\)/i",$img_happy,$text);
  echo $text;
Thanx!
you are welcome :)
What are your view on PHP-Eclipse? I'm using Dreamweaver 2004MX
yeah it is not bad, personally I don't use that one but it is just a question of tastes :-)
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: alextr2003fr {http:#13780598}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer