Worqy Posted February 26, 2010 Share Posted February 26, 2010 Hello. Now I have this problem: I need to get some values for 4 labels in my document main.php, form the document checkstatus.php Now, when you open my site you come to main.php, and there I got a href to the site checkstatus.php which get some data from MySQL, and now I need to post these php variabels in a html label... //Kevin ps. I tryed to use include '...'; but the problem is that it then also show the html code in checkstatus.php Link to comment https://forums.phpfreaks.com/topic/193479-problems/ Share on other sites More sharing options...
jl5501 Posted February 26, 2010 Share Posted February 26, 2010 Can you show us the code? Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018568 Share on other sites More sharing options...
Worqy Posted February 26, 2010 Author Share Posted February 26, 2010 main.php <?php // Server 1, main.php Session_start(); if($_SESSION['LoginS1'] == false) { ?> <html> <head> <title>Access denied!</title> </head> <body> <center>Access denied!</center> <p> Please login to view this site! <p> <a href="/Game/login.php">Login</a> </body> </html> <?php } else { ?> <html> <head> <title>Welcome</title> </head> <body> <center> </center> <p> <center><a href="checkstatus.php">Check</a></center> <a href="/Game/logout.php">Logout</a> </body> </html> <?php } ?> checkstauts.php <?php // S1, checkstatus.php // Starts sesson Session_start(); include 'connect.config.php'; // Checks if SESSION LoginS1 = true if($_SESSION['LoginS1'] == true) { //Connect to MySQL and select Database mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select database!"); $data = mysql_query("SELECT * FROM `resoreces` WHERE 1") or die(mysql_error()); // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['username'] = $S1three; $info['username'] = $S1clay; $info['username'] = $S1iron; $info['username'] = $S1wheat; ?> <html> <form action="main.php"> <body> <input type="submit" name="test" value="refresh"> </body> </form> </html> <?php } else { // If SESSION LoginS1 = false echo 'Acces Denied!'; } ?> Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018690 Share on other sites More sharing options...
jl5501 Posted February 26, 2010 Share Posted February 26, 2010 It is not clear what you are trying to do here. In your checkstatus.php you are gathering some data from the database and populating an array. You do not seem to be attempting to display the array contents anywhere in the html Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018710 Share on other sites More sharing options...
Worqy Posted February 27, 2010 Author Share Posted February 27, 2010 Sorry, I uploaded the version without the labels and stuff.. What I'm trying to do is that checklogin.php is getting some data ($S1username..) and I want to show the values in main.php Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018948 Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 This code is overwriting the username element of the $info array with each assignment, so you will end up with the last item only. Also, you will need to load the array into the session, to have it available on the other page. $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['username'] = $S1three; $info['username'] = $S1clay; $info['username'] = $S1iron; $info['username'] = $S1wheat; so this is one way of loading the array $info['username'][] = $S1username; $info['username'][] = $S1three; $info['username'][] = $S1clay; $info['username'][] = $S1iron; $info['username'][] = $S1wheat; which would give you numbered elements addressed as $info['username'][0] etc. or youi can do this $info['username']['name'] = $S1username; $info['username']['three'] = $S1three; $info['username']['clay'] = $S1clay; $info['username']['iron'] = $S1iron; $info['username']['wheat'] = $S1wheat; Then say $_SESSION['info'] = $info; And in main.php say $info = $_SESSION['info']; to get it back Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018951 Share on other sites More sharing options...
Worqy Posted February 27, 2010 Author Share Posted February 27, 2010 Now I found one off the biggest misstakes... $info['username'] = $S1username; $info['username'] = $S1three; $info['username'] = $S1clay; $info['username'] = $S1iron; $info['username'] = $S1wheat; shall be: $info['username'] = $S1username; $info['three'] = $S1three; $info['clay'] = $S1clay; $info['iron'] = $S1iron; $info['wheat'] = $S1wheat; Updated code: main.php <?php // Server 1, main.php Session_start(); if($_SESSION['LoginS1'] == false) { ?> <html> <head> <title>Access denied!</title> </head> <body> <center>Access denied!</center> <p> Please login to view this site! <p> <a href="/Game/login.php">Login</a> </body> </html> <?php } else { ?> <html> <head> <title>Welcome</title> </head> <body> <center> <label name="three" id="three">100</label> </center> <p> <center><a href="checkstatus.php">Check</a></center> <a href="/Game/logout.php">Logout</a> </body> </html> <?php } ?> checkstatus.php <?php // S1, checkstatus.php // Starts sesson Session_start(); include 'connect.config.php'; // Checks if SESSION LoginS1 = true if($_SESSION['LoginS1'] == true) { //Connect to MySQL and select Database mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select database!"); $data = mysql_query("SELECT * FROM `resoreces` WHERE 1") or die(mysql_error()); // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['three'] = $S1three; $info['clay'] = $S1clay; $info['iron'] = $S1iron; $info['wheat'] = $S1wheat; ?> <html> <form action="main.php"> <body> <input type="submit" name="test" value="refresh"> </body> </form> </html> <?php } else { // If SESSION LoginS1 = false echo 'Acces Denied!'; } ?> Now I made a label in main.php and I want to fill in the value which checkstatus.php gets from MySQL in it Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018971 Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 so now you are correctly populating your $info array. Next you need to add it to the session in the way I described, and get it from the session in main.php Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1018998 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 OK I'm quite new to PHP and even more new to sessions. Can you just show a example, then I think I will figure it out Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019310 Share on other sites More sharing options...
Altec Posted February 28, 2010 Share Posted February 28, 2010 OK I'm quite new to PHP and even more new to sessions. Can you just show a example, then I think I will figure it out Here you go. Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019317 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 ok, now I have tried something. doesnt really work: main.php <?php // Server 1, main.php Session_start(); if($_SESSION['LoginS1'] == false) { ?> <html> <head> <title>Access denied!</title> </head> <body> <center>Access denied!</center> <p> Please login to view this site! <p> <a href="/Game/login.php">Login</a> </body> </html> <?php } else { ?> <html> <head> <title>Welcome</title> </head> <body> <center> <?php $_SESSION['ThreeS1'] = $S1three; echo "Three: $S1three"; ?> It just showes this: "Three:" and then nothing... </center> <p> <center><a href="checkstatus.php">Check</a></center> <a href="/Game/logout.php">Logout</a> </body> </html> <?php } ?> checkstatus.php <?php // S1, checkstatus.php // Starts sesson Session_start(); include 'connect.config.php'; // Checks if SESSION LoginS1 = true if($_SESSION['LoginS1'] == true) { //Connect to MySQL and select Database mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select database!"); $data = mysql_query("SELECT * FROM `resoreces` WHERE 1") or die(mysql_error()); // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['three'] = $S1three; $info['clay'] = $S1clay; $info['iron'] = $S1iron; $info['wheat'] = $S1wheat; $_SESSION['ThreeS1'] = $S1three ?> <html> <form action="main.php"> <body> <input type="submit" name="test" value="refresh"> </body> </form> </html> <?php } else { // If SESSION LoginS1 = false echo 'Acces Denied!'; } ?> Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019324 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 use if(isset($_SESSION['LoginS1])) { insted of if($_SESSION['LoginS1'] == false) { With Love Coding Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019325 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 ok, but thats not the real problem here. Check out the $_SESSION['ThreeS1'] insted BTW: I tryed if(isset($_SESSION['LoginS1])) { But I get the message "Access denied!" Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019457 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 ok, but thats not the real problem here. Check out the $_SESSION['ThreeS1'] insted BTW: I tryed if(isset($_SESSION['LoginS1])) { But I get the message "Access denied!" this one is also incorrect if(isset($_SESSION['LoginS1'])) { check single quotes Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019481 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 ok, but thats not the real problem here. Check out the $_SESSION['ThreeS1'] insted BTW: I tryed if(isset($_SESSION['LoginS1])) { But I get the message "Access denied!" this one is also incorrect if(isset($_SESSION['LoginS1'])) { check single quotes Does not still work. Maby the problem is that when you login with the correct username and password this happens: $_SESSION['LoginS1'] = true; Maby I shall insted og making it true, register it? Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019490 Share on other sites More sharing options...
Andy-H Posted February 28, 2010 Share Posted February 28, 2010 You haven't grasped the proper use of the assignment operator. It applies a copy of the variable's data on the right side of the operator to the variable on the left side of the operator. http://uk2.php.net/manual/en/language.operators.assignment.php Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019513 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 You haven't grasped the proper use of the assignment operator. It applies a copy of the variable's data on the right side of the operator to the variable on the left side of the operator. http://uk2.php.net/manual/en/language.operators.assignment.php Ok? I read the text in the link, understood almost everything in it. But I still dont know how to continue Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019519 Share on other sites More sharing options...
Andy-H Posted February 28, 2010 Share Posted February 28, 2010 // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['username'] = $S1three; $info['username'] = $S1clay; $info['username'] = $S1iron; $info['username'] = $S1wheat; You are first populating your info variable with an array of data returned by mysql_fetch_array then assigning variables (which I am assuming are defined in your connect.config.php file?) to the array key username an repeatidly overwriting it with other variables. What exactly are you trying to do as your code doesn't make it clear?? Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019530 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data ); $info['username'] = $S1username; $info['username'] = $S1three; $info['username'] = $S1clay; $info['username'] = $S1iron; $info['username'] = $S1wheat; You are first populating your info variable with an array of data returned by mysql_fetch_array then assigning variables (which I am assuming are defined in your connect.config.php file?) to the array key username an repeatidly overwriting it with other variables. What exactly are you trying to do as your code doesn't make it clear?? What I'm trying to do is get the data from MySQL (in checkstatus.php), put it into $S1username, $S1three ... and then by a Session get it to main.php and show it over there.. The only thing I get from connect.config.php is the value $host, $username, $password... for connecting to MySQL BTW: You quoted from wrong message, look at reply #10 And thank you all for helping me, sorry if I dont understand most of it =) Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019533 Share on other sites More sharing options...
Andy-H Posted February 28, 2010 Share Posted February 28, 2010 I would do it this way. in checkstatus.php // Puts the data from "resorces" into an object ($info) $info = mysql_fetch_object( $data ); $_SESSION['userdata'] = serialize($info); now in main.php session_start(); $userdata = isset($_SESSION['userdata']) ? unserialize($_SESSION['userdata']) : false; if ($userdata) { // now to access username use $userdata->username; //etc... } Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019543 Share on other sites More sharing options...
Worqy Posted February 28, 2010 Author Share Posted February 28, 2010 I would do it this way. in checkstatus.php // Puts the data from "resorces" into an object ($info) $info = mysql_fetch_object( $data ); $_SESSION['userdata'] = serialize($info); In this, it seems like you have forgot the $S1three, $S1clay ? now in main.php session_start(); $userdata = isset($_SESSION['userdata']) ? unserialize($_SESSION['userdata']) : false; if ($userdata) { // now to access username use $userdata->username; //etc... } Hmm... I really dont understand anything about this... Is not there a easyer way, like in; Just get the data from the session and show it, because I think you maby have messed up the really problem with my login script thing.... I just want to post the value of $S1three(checkstatus.php) in main.php Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019551 Share on other sites More sharing options...
Andy-H Posted February 28, 2010 Share Posted February 28, 2010 In that case just do this. checkstatus.php // Puts the data from "resorces" into a array ($info) $info = mysql_fetch_array( $data, MYSQL_ASSOC ); $S1username = $info['username']; $S1three = $info['three']; $S1clay = $info['clay']; $S1iron = $info['iron']; $S1wheat = $info['wheat']; //set session for use on main.php $_SESSION['ThreeS1'] = $S1three; main.php session_start(); $S1three = isset($_SESSION['ThreeS1']) ? $_SESSION['ThreeS1'] : 'Default'; Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1019576 Share on other sites More sharing options...
Worqy Posted March 2, 2010 Author Share Posted March 2, 2010 Now I have found the problem. I tested a bit with this code: checkstatus.php .... $_SESSION['test'] = 'kevin'; .... main.php .... echo $_SESSION['test']; .... And it worked perfectly, so I think the problem in the real code is here: checkstatus.php .... $_SESSION['username'] = $info['username']; .... I think my code works, but that the session S1username 's value is nothing. Full code: checkstatus.php <?php // Server 1, checklogin.php Session_start(); include "connect.config.php"; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select Database"); $data = mysql_query("SELECT * FROM `resoreces`") or die(mysql_error()); Print "<table border cellpadding=3>"; while($info = mysql_fetch_array( $data )) { Print "<tr>"; Print "<th>Username:</th> <td>".$info['username'] . "</td>"; Print "</tr><tr>"; Print "<th>Gold:</th> <td>".$info['gold'] . "</td>"; Print "</tr><tr>"; Print "<th>Three:</th> <td>".$info['three'] . "</td> "; Print "</tr><tr>"; Print "<th>Clay:</th> <td>".$info['clay'] . "</td> "; Print "</tr><tr>"; Print "<th>Iron:</th> <td>".$info['iron'] . "</td> "; Print "</tr><tr>"; Print "<th>Wheat:</th> <td>".$info['wheat'] . " </td>"; Print "</tr>"; $_SESSION['S1username'] = $info['username']; } ?> <html> <body> <a href="main.php">Back</a> </body> </html> <?php Print "</table>"; ?> main.php <?php // Server 1, main.php Session_start(); if($_SESSION['LoginS1'] == false) { ?> <html> <head> <title>Access denied!</title> </head> <body> <center>Access denied!</center> <p> Please login to view this site! <p> <a href="/Game/login.php">Login</a> </body> </html> <?php } else { ?> <html> <head> <title>Welcome</title> </head> <body> <center> <?php echo 'Username: '; echo $_SESSION['S1username']; ?> </center> <p> <center><a href="checkstatus.php">Check</a></center> <a href="/Game/logout.php">Logout</a> </body> </html> <?php } ?> So anyone here witha solution for the S1username session problem? Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1020584 Share on other sites More sharing options...
Worqy Posted March 3, 2010 Author Share Posted March 3, 2010 No one? Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1021031 Share on other sites More sharing options...
Worqy Posted March 7, 2010 Author Share Posted March 7, 2010 I havent been online for some days now because my PC crashed. I'm still in the same place, havent made any progress on the problem. Hope there is someone that want to help me with this! //Kevin Link to comment https://forums.phpfreaks.com/topic/193479-problems/#findComment-1022737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.