justsomeguy Posted March 12, 2008 Share Posted March 12, 2008 Don't put the function definition inside an if statement. Put the function definition inside an include file or something and include it on every page that you want to use the function. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 Ok. I put the session_redirect script inside session_redirect.php and in the checklogin.php I putinclude("session_redirect.php"); at the top. When the if statement is true, I put the line session_redirect('login_success.php'); and thats it. Still comes up a blank screen... I should probably put $_SESSION['myusername'] = '$myusername'; before the session_redirect function call Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2008 Share Posted March 12, 2008 You still need the header name.session_redirect('Location: login_success.php'); Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 Yep, that did it. Unfortunately, the session still has no value. Comes up with "Array ()".I have $_SESSION['myusername'] = '$myusername'; before the session_redirect call. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2008 Share Posted March 12, 2008 Post your code now. Also, if you're just assigning a variable as a value, don't quote it. And if you do have a string that has a variable in it, don't use single quotes, they won't replace the variable. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 K. This is the checklogin.php script: <?phpinclude("session_redirect.php");ob_start();$host="localhost"; // Host name$username="gdhghfh"; // Mysql username$password="dfghdhd"; // 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 and redirect to file "login_success.php"$_SESSION['myusername'] = $myusername;session_redirect('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 12, 2008 Share Posted March 12, 2008 You need to use session_start on every page that reads or writes from the session. I don't see it on that page. I think you missed an equal sign on this line also:$_SESSION['myusername'] $myusername; Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 Ya, I accidentally deleted the = at first, I edited it real quick though, a lil too quick..I added session_start(); and it works, but now the login_success.php doesn't show anything where I had the session name to be shown. No "Array ()" or anything. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 Nevermind my last post, it seems that instead of going to login_success.php and staying, the PHP to check if the session exists seems to be having a problem and sends me to the index. When a user goes to a page that requires to be logged in, it redirects to the index.php. BUT, my index.php will display two different pages depending on if your logged in or not, and it seems to be displaying the one saying I'm logged in. o_o Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 ... Nevermind about the logged in part. I changed the "Is Logged In" part of the index to say "Not Logged In" and re-logged in, and it says Not Logged in :| Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2008 Share Posted March 12, 2008 So what's the rest of the code? What is the code that checks if someone is logged in, redirects, etc, all the code in play. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 I'll post it in a bit. My index is having the "switched around" problem where if your not logged in it says you are... It switches around for some reason.K, this 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");}?> It redirects me to the index after I log in, and the index says I'm not logged in either Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 12, 2008 Author Share Posted March 12, 2008 The bell rings yet again. BB tomorrow :[ Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2008 Share Posted March 12, 2008 Your if statement is backwards. If the session is not set then it shows the "login successful" HTML page. If it is set then it redirects to the index page. Link to comment Share on other sites More sharing options...
ali_ggl Posted March 13, 2008 Share Posted March 13, 2008 I think in PHP we cant use the variable name as $name. check it hope it will work.Ali. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2008 Share Posted March 13, 2008 $name is ok to use as a variable. The only variables I can think of that are reserved, other then the superglobals like $_POST and $_SESSION, are $this, $php_errormsg, and $http_response_data. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 13, 2008 Author Share Posted March 13, 2008 I tried your suggestion, but it seems the session is not being made or the index.php is.. messed up.When you first visit the index, it states "Not Logged In" in the body table. If you are logged in, it says "Logged In".After I log into the site, I am redirected to the index and it says "Not Logged In" so I guess the changes I've made to the session seems to have stopped working. Idk.. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 13, 2008 Author Share Posted March 13, 2008 Ok! I got it now. I guess I removed session_start(); on accident so I added it back again but I also included the $myusername variable inside the if statement just in case it happened to get lost.Now "Array ( [myusername] => TehBlizzy )" appears at the bottom of the page. I'm guessing thats right?--Edit...Oh, and btw, this was w/o using your session_redirect function. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2008 Share Posted March 13, 2008 Yeah I don't need that redirect function much, I've had to use it in one place so I thought it might be worth a shot. It sounds like it's working now though. Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 13, 2008 Author Share Posted March 13, 2008 Yep. The session name works fine now. Unfortunately, something seems to be wrong in my posting script. I have the username in a will-be hidden text box. The problem is I can only get one variable in the message to work. If I just put "lol says: $msg" it appears as "lol says: (the message)" in the chat file, and if I do "$username says: lol" it works that way too, but if I put both "$username says: $msg" it doesn't work... but thats how it is in the original working chatroom code I made. Wtf? o_0 <?php$msg = $_POST["UserMsg"];$username = $_POST["Username"];$file = fopen("chat_text/room1.htm","a+");fwrite($file,"$username says : $msg <br>");fclose($file);?> Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 13, 2008 Author Share Posted March 13, 2008 Curse you bell! I manages to get "$username $msg" to appear, but if I add says: into it, it won't work. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2008 Share Posted March 13, 2008 That doesn't make sense. Add brackets for clarity.fwrite($file,"{$username} says : {$msg} <br>"); Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 14, 2008 Author Share Posted March 14, 2008 Sweet. It works now. >>All that is left is trying to get my registering code to work :)Oh, and I'm looking for ways to prevent HTML from being sent in, so people can't change their text colors and mess it up. >>--EditWell not really "prevent" html from being sent, but replace any <'s and >'s with < and > and remove any other possible bad data such as PHP and stuff Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 14, 2008 Author Share Posted March 14, 2008 The register script works now, but I'm making it so if its empty or the username/password is taken, it will redirect to an error page. If the $myusername or $mypassword field is empty, it redirects to the empty error page.But, the problem is, the part where it checks if the Username/Password is taken, it will redirect to the taken error page, but if the person types in a different password than what is registered, it takes them to the empty error page. if($count==1){header("location:register_err.php?err=taken");}else if(empty($myusername[$mypassword])) {header("location:register_err.php?err=empty");} If you want to experience it yourself, go to randomchatrooms.zxq.net/register.php and type the username "test" and the password "test". After that, hit the back button and type in username"test" but password "tst" or something else and see for yourself :S Link to comment Share on other sites More sharing options...
TehBlizzy Posted March 14, 2008 Author Share Posted March 14, 2008 Ahahah... nevermind that. I used the same code as the login code making it grab the username AND password data. I just removed the password part and it works fine now.All I need now is someone to help me replace < and > with < and >.. and I guess remove other PHP things for the chatrooms from the message that someone sends.Oh, and something that checks if someone tries to register a name and password with unwanted characters Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.