Jump to content

Problems...


Worqy

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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?  :confused:

I read the text in the link, understood almost everything in it.

But I still dont know how to continue  :shrug:

Link to comment
Share on other sites

// 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
Share on other sites

// 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
Share on other sites

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.