TehBlizzy Posted March 7, 2008 Share Posted March 7, 2008 I've been making a site using my Random Chatroom stuff and its going good. One thing though, I'm stuck on getting the session name.I tried this on my posting PHP script but the username comes out as the default PHPSESSID because the actually user never visits the page to give the session name to the script: <?php $msg = $_POST["UserMsg"];$name = session_name();$file = fopen("chat_text/room1.htm","a+");fwrite($file,"$name says: $msg <br>");fclose($file);?> Since that didn't work, I tried getting the session name through Javascript but that didn't work either. At the moment I'm trying to get the PHP script to write the session_name() value into a will-be hidden field for the AJAX script to grab and send to the posting script. This is my chatroom page code <?session_start();if(!session_is_registered(myusername)){header("location:http://randomchatrooms.zxq.net/index.php");}else {echo("<html> <head> <meta http-equiv='content-type' content='text/html;charset=iso-8859-1'> <title>Random Chatrooms - Room One</title><link rel='stylesheet' href='css/default.css' type='text/css'> </head><script style='javascript'>function ajaxFunction(){ var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { alert('Your browser does not support AJAX!'); return false; } } } data = ''; data += '&UserMsg=' + escape(document.getElementById('UserMsg').value); data += '&Username=' + escape(document.getElementById('Username').value); xmlHttp.open('POST','room1_post.php',true); xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlHttp.setRequestHeader('Content-length', data.length); xmlHttp.send(data); parent.the_text.window.location.reload; parent.the_text.window.scroll(0,99999); document.Msg.UserMsg.value='' document.getElementById('msg_id').focus();}</script> <body bgcolor='000000' onLoad='lol()'> <center> <table width='800' border='1' height='500' bordercolor='FF0000'> <tr height='50'> <td colspan='2'> <center> <h3>Random Chatrooms - Room One</h3> </center> </td> </tr> <tr height='400'> <td colspan='2'> <div align='center'> <iframe id='the_text' name='the_text' src='chat_text/room1.htm' height='350' width='650' border='0'></iframe></div> </td> </tr> <tr> <td><div align='center' valign='middle'><br> <form name='Msg' method='post' onsubmit='ajaxFunction(); return false;' action=''> Message: <input id='UserMsg' type='text' name='UserMsg' size='70' maxlength='60' id='msg_id'> <input type='submit' value='Send!'><input type='text' name='Username' value='session_name()' id='Username'> </form></div> </td> </tr> </table> </body></html>");}?> Unfortunately, the Username field comes up with the value of exactly session_name()Can anyone tell me how to get the session name into the Username box? >_< Link to comment Share on other sites More sharing options...
Synook Posted March 8, 2008 Share Posted March 8, 2008 ??? Why don't you just use the $_SESSION superglobal? So when they login you do session_start; //This goes at the VERY top of the page//... code etc.$_SESSION['name'] = $name; //Setting name to the right value Then when they enter a message you do <?phpsession_start();$msg = $_POST["UserMsg"];$name = $_SESSION['name'];$file = fopen("chat_text/room1.htm","a+");fwrite($file,"$name says: $msg <br>");fclose($file);?> Link to comment Share on other sites More sharing options...
jlhaslip Posted March 8, 2008 Share Posted March 8, 2008 You do realize that session_name is a php function to 'name' the cookie and not to retrieve the 'user-name' stored in the session array?Try a print_r($GLOBALS) to display the values stored and you should see what I mean.http://php.net/session_name Link to comment Share on other sites More sharing options...
justsomeguy Posted March 8, 2008 Share Posted March 8, 2008 Also, instead of this: <?session_start();if(!session_is_registered(myusername)){header("location:http://randomchatrooms.zxq.net/index.php");} do this: <?phpsession_start();if(!isset($_SESSION['name'])){header("location:http://randomchatrooms.zxq.net/index.php");} Use the long PHP tags and it's better to stay away from using the session register functions, they have been deprecated. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 10, 2008 Share Posted March 10, 2008 doesn't the session_name() need to be used before the session_start() ??? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 Only if you're using the function to set the name. If you're reading it then it doesn't matter when you use it. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 dup Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 10, 2008 Author Share Posted March 10, 2008 Sweet =PI'll try all of that today, Ty for the help Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 10, 2008 Author Share Posted March 10, 2008 I had to change the isset to redirect if it wasn't set and that part works fine now. I changed the session register part of the login script from something like session_register to ...$_SESSION['name'] = $myusername;header("location:login_success.php");... .... Dang bell rang, I'll finish tomorrow Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 Anyways, the message sending script works and all, but the message doesn't contain a name, all it says is "says: test"This is my posting script: <?phpsession_start();$msg = $_POST["UserMsg"];$name = $_SESSION['name'];$file = fopen("chat_text/room1.htm","a+");fwrite($file,"$name says: $msg <br>");fclose($file);?> and this is the chatroom code now: <?phpsession_start();if(!isset($_SESSION['myusername'])){}else {header("location:http://randomchatrooms.zxq.net/index.php");}?><html> <head> <meta http-equiv='content-type' content='text/html;charset=iso-8859-1'> <title>Random Chatrooms - Room One</title><link rel='stylesheet' href='css/default.css' type='text/css'> </head><script style='javascript'>function ajaxFunction(){ var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { alert('Your browser does not support AJAX!'); return false; } } } data = ''; data += '&UserMsg=' + escape(document.getElementById('UserMsg').value); xmlHttp.open('POST','room1_post.php',true); xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlHttp.setRequestHeader('Content-length', data.length); xmlHttp.send(data); parent.the_text.window.location.reload; parent.the_text.window.scroll(0,99999); document.Msg.UserMsg.value='' document.getElementById('msg_id').focus();}</script> <body bgcolor='000000' onLoad='lol()'> <center> <table width='800' border='1' height='500' bordercolor='FF0000'> <tr height='50'> <td colspan='2'> <center> <h3>Random Chatrooms - Room One</h3> </center> </td> </tr> <tr height='400'> <td colspan='2'> <div align='center'> <iframe id='the_text' name='the_text' src='chat_text/room1.htm' height='350' width='650' border='0'></iframe></div> </td> </tr> <tr> <td><div align='center' valign='middle'><br> <form name='Msg' method='post' onsubmit='ajaxFunction(); return false;' action=''> Message:Â <input id='UserMsg' type='text' name='UserMsg' size='70' maxlength='60' id='msg_id'> <input type='submit' value='Send!'> </form></div> </td> </tr> </table> </body></html> I have no clue what the prob is... oh and this is what the login part is ...$_SESSION['name'] = $myusername;header("location:login_success.php");... $myusername is the variable used to check for the account and the account's username value Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 It looks like you're trying to use $_SESSION['name'], $_SESSION['myusername'], and session_name() for the same thing. Be consistent with your naming, and the session_name function is only going to return something like "PHPSESSID", not the user's name. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 I changed everything but the posting script's session thing to myusername and the chat still sent the message, but when I changed the posting script's session to myusername it doesn't work at all. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 I'm returning to my first way of echoing the page out with the will-be hidden text box to have the $_SESSION['myusername'] value in it, but my first problem is I can't use double-quotes in the page and putting a \ in front doesn't work because the hosting service seems to remove them when saved. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 Can you post what you're trying to do? Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 <input type='text' name='Username' value='(session name)' id='Username'> Thats basically the will-be hidden text box echo'ed by the PHP script. I'm trying different values for it but none work so far :SWhen the message is sent, the AJAX grabs the will-be hidden text box's value and post it to the msg posting script Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 I tried putting in just $_SESSION as the value and the value came out as "Array" in the text box, so I tried putting value=\"$_SESSION['myusername']\" and it comes up with "Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /www/zxq.net/r/a/n/randomchatrooms/htdocs/chatrooms/room_one.php on line 78"I just tried removing the 's is 'myusername' and the page loads, but the text box has no value in it Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 Well, it will be better to use the session then a hidden input. After someone logs in and gets redirected use print_r($_SESSION) to make sure the session is being set properly. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 It came out as "print_r(Array)" on the page... I probably didn't put it in right though... The login_success.php page echoes out the page if the session exists so I just put print_r($_SESSION) inside the echo'ed HTML page.. I'm guessing thats not right =S---Seconds Later:I put the print_r($_SESSION) outside the HTML inside a different echo cmd and it still came out as print_r(Array) Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 Don't echo it.<?phpprint_r($_SESSION);?> Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 I tried that a while ago. "Array ()" is all that appears on the pageThis is the login_success.php page: <?phpsession_start();if(!isset($_SESSION['myusername'])){echo("<html><head><title>Random Chatrooms - Login Successful</title><link href='css/default.css' rel='stylesheet' type='text/css'></head><body><center><table border='1' width='800'><tr><td colspan='3' height='150' width='800'>Header</td></tr><tr height='600'><td width='100'><div align='center'><h2>Navigation</h2><br><br><a href='index.php'>Index</a><br><a href='updates.php'>Updates</a><br><a href='rooms.php'>Chatrooms</a><br><a href='profile.php'>My Profile</a><br><a href='viewpf.php'>View Profile</a><br><a href='logout.php'>Logout</a></div></td><td width='550'>Login Successful!<br></td><td>Ads</td></tr></table></body></html>");print_r($_SESSION);}else {header("location:http://randomchatrooms.zxq.net/index.php");}?> and this is my checklogin.php script w/o the passwords and all (of course =P) <?phpob_start();$host="localhost"; // Host name$username="hggdfh"; // Mysql username$password="dghfdfgh"; // Mysql password$db_name="randomchatrooms_zxq_net_login"; // Database name$tbl_name="members"; // Table name// Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");// Define $myusername and $mypassword$myusername=$_POST['myusername'];$mypassword=$_POST['mypassword'];$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"$_SESSION['myusername'] = $myusername;header("location:login_success.php");}else {echo "Wrong Username or Password";}ob_end_flush();?> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 OK, then the session is empty. First, make sure you're using session_start on every page. If that's not working, there are some issues with redirecting right after you set session variables. You can try to use this function to redirect instead of header to make sure the session ID goes with the redirect. function session_redirect( $location ){ $sname = session_name(); $sid = session_id(); if( strlen( $sid ) < 1 ) { header( $location ); return; } if( isset( $_COOKIE[ $sname ] ) || strpos( $location, $sname."=".$sid ) !== false ) { header( $location ); return; } else { if( strpos( $location, "?" ) > 0 ) $separator = "&"; else $separator = "?"; $fixed = $location . $separator . $sname."=".$sid; header( $fixed ); return; }} Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 Does that go inside the checklogin.php script page?and ya, all my pages have session_start(); at the top so it can check if it is set and if not it redirects to the index, unless it has to be put into the echo'ed HTML page as well... Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 11, 2008 Author Share Posted March 11, 2008 Curse school. I shall be back tomorrow Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 Does that go inside the checklogin.php script page?It doesn't matter where you define it, as long as every script that needs to use it has access to it. Use session_redirect instead of header. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 Well, when the user clicks the log in button, it calls the script and after all the MySQL work it checks if the username and password are on the same row. If they are, it originally has header('login_success.php') and it echo's "Wrong Username or Password" if otherwise.So I basically removed the header cmd and copied your suggested script into the if statement, then put the line "session_redirect('login_success.php')" underneath it to call the function.It comes up with a blank screen and the url is of the checklogin.php script. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.