Author Topic: variables not being passed  (Read 933 times)

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
variables not being passed
« on: October 02, 2010, 08:15:37 AM »
My login script doesnt pass variables to the other pages

login:
Code: [Select]
<?
include('Config.php');
if ($_POST['username'] && $_POST['password']) {
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);

$checklogin = mysql_query("SELECT username FROM users WHERE username = '".$username."' AND password = '".$password."'");

$result2 = mysql_num_rows($checklogin);
echo $checklogin;
if($result2 == 0) {
echo "Login Failed";
}else{

$getuserinfo = "SELECT * FROM users WHERE username='$_POST[username]'";
$resultuserinfo = mysql_query($getuserinfo);

$row = mysql_fetch_assoc($resultuserinfo);
$_SESSION['authenticated']=true;
$_SESSION['username'] = $username;
    $_SESSION['pass'] = $password;
$_SESSION['userid'] = $row['id'];
if(!isset($_COOKIE['village'])){
$_COOKIE['village'] = 1;
}

header('Location:Village.php');

}

}else{

?>

<h2>Site Login</h2>
<form action="login.php" method="post">
Username: <input type="text" name="username" maxlength="10" size="24"><br>
Password: <input type="password" name="password" maxlength="10" size="25"><br><br>
<input type="submit" name="submit" value="Login">
<input type="reset" name="reset" value="Reset">
</form>
<br><br>
<a href="register.php">Register</a>

<? } ?>

Reciving page:
Code: [Select]
<?php
include('Config.php');

if (!isset(
$_SESSION['authenticated'])){
 echo "<meta http-equiv=\"refresh\" content=\"1;url=http://argentwars.scireportals.com/login.php\">";
}
$userid $_SESSION['user'];
$village $_COOKIE['village'];
$vquery "SELECT * FROM villages WHERE player=$userid and number=$village LIMIT 1";
$vresult mysql_query($vquery);

$row mysql_fetch_assoc($vresult);

$wood $row['wood'];
$clay $row['clay'];
$iron $row['iron'];
$stone $row['stone'];
$market $row['market'];
$barracks $row['barracks'];
?>


Welcome, <? echo $_SESSION['username']; ?><br>
Wood: <? echo $wood; ?><br>
Clay: <? echo $clay; ?><br>
Iron: <? echo $iron; ?><br>
Stone: <? echo $stone; ?><br>
<a href="Market.php">Your Market Level is: <? echo $market; ?></a><br>
<a href="Barrack.php">Your Barracks Level is: <? echo $barracks; ?></a><br>

<?






?>

Config.php:
Code: [Select]
<?php
session_start
();
$dbhost '*********';
$dbuser '*******';
$dbpass '*******';
$dbname '******'; // our database settings
$conn mysql_connect($dbhost,$dbuser,$dbpass)
or die('Error connecting to mysql');
mysql_select_db($dbname);

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: variables not being passed
« Reply #1 on: October 02, 2010, 08:34:05 AM »
Code: [Select]
$village = (int)$_COOKIE['village'];
$vquery = "SELECT * FROM villages WHERE player=$userid and number=$village LIMIT 1";
or
Code: [Select]
$village = $_COOKIE['village'];
$vquery = "SELECT * FROM villages WHERE player=$userid and number='$village' LIMIT 1";
Even if you use magic quotes you need to either convert all integers to integers or add quotes for integer variables in SQL "number='$village' ". People can set cookie to "0; DROP TABLE villages; SELECT * FROM villages " (no quotes needed to crash your server in this case :)).

Always add exit(); after header('Location:xxx.php'); redirect do not stop parsing the old page!

To track your problem remove Header and put echo to see if variables are OK.

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #2 on: October 02, 2010, 11:57:29 AM »
1. Thanks for that adivce
2. see above
3. I allready did that. All session variables and cookie variables are compkletly blank

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: variables not being passed
« Reply #3 on: October 02, 2010, 12:11:36 PM »
Quote
            $getuserinfo = "SELECT * FROM users WHERE username='$_POST[username]'";
            $resultuserinfo = mysql_query($getuserinfo);
            
            $row = mysql_fetch_assoc($resultuserinfo);
            $_SESSION['authenticated']=true;
            $_SESSION['username'] = $username;
This part is inconsistent. Decide to use either $_POST[username] or $username, not both.
(while cleaning that part you should find out why the code don't work :D)

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #4 on: October 02, 2010, 12:24:20 PM »
ah, thanks for that. But it didnt do anything

Offline Winawer

  • Level 6
  • *
  • Posts: 27
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #5 on: October 02, 2010, 12:42:06 PM »
You're saving the user id into $_SESSION['userid'] at login, but trying to read it from $_SESSION['user'] on the village page. Maybe this is the problem?

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #6 on: October 02, 2010, 12:59:01 PM »
another error, and thanks for the help, but it still doesnt work. Also, the sesson variables are there on the login page, just not the other pages

Offline Winawer

  • Level 6
  • *
  • Posts: 27
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #7 on: October 02, 2010, 01:07:06 PM »
What kinds of errors are you getting and what does a var_dump($_SESSION) in Village.php print out?

Offline SpaceDoG

  • Level 5
  • *
  • Posts: 20
  • Reputation: +1/-0
    • View Profile
Re: variables not being passed
« Reply #8 on: October 02, 2010, 01:15:33 PM »
trying to use $_SESSION without doing a session_start() will not work. You have to do declare session_start before you attempt to use $_SESSION.

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #9 on: October 02, 2010, 02:09:12 PM »
Ideclaired it in the config.php file

Offline SpaceDoG

  • Level 5
  • *
  • Posts: 20
  • Reputation: +1/-0
    • View Profile
Re: variables not being passed
« Reply #10 on: October 02, 2010, 02:26:15 PM »
Ok. Also to use $_COOKIE you first have to do cookie_set()... Then you can use $_COOKIE.

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #11 on: October 02, 2010, 03:18:51 PM »
atal error: Call to undefined function cookie_set() in /home/argentwars/argentwars.scireportals.com/Config.php on line 4
It gives me this error

Offline Winawer

  • Level 6
  • *
  • Posts: 27
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #12 on: October 02, 2010, 03:40:46 PM »

Offline SpaceDoG

  • Level 5
  • *
  • Posts: 20
  • Reputation: +1/-0
    • View Profile
Re: variables not being passed
« Reply #13 on: October 02, 2010, 03:59:34 PM »
err yea.. my bad :-P sorry bit drunk and it was like 10pm wehen I said that :-P

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: variables not being passed
« Reply #14 on: October 02, 2010, 04:38:57 PM »
ah, fixed it. Thanks so much for all your help.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal