Jump to content

need help fixing this php script


Jiraiya

Recommended Posts

  • Replies 67
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Now before we can do anything you need to backtrack and find where the cookie is set - looks like it'll be in another script somewhere BUT... as I said earlier, you're not accessing it.

 

You could fill it with some junk at the start of the script just to test but if that cookie is empty chances are the rest are also empty which is why you need to back track - Maybe start the whole site again so it is defined?

Link to comment
Share on other sites

you mean i have to start my game over from the beginning? is there another way i could create this battle script without having to start all over again??

 

oh is this what you wanted to see for the cookie??

 

echo "3";
   $UserSQuery = mysql_query("SELECT * FROM `users` WHERE username='$username'");
   $UserS = mysql_fetch_array($UserSQuery);

   $NPCSQuery = mysql_query("SELECT * FROM `npc` WHERE npcname='$NPC'");
   $NPCS = mysql_fetch_array($NPCSQuery);

   setcookie('NPC', $NPC, 2400);
   setcookie('health', $UserS['health'], 2400);
   setcookie('skill', $UserS['skill'], 2400);
   setcookie('NPCH', $NPCS['health'], 2400);
   }

?>

Link to comment
Share on other sites

No, just "mentally" read through the other scripts and see what data is being set inside the cookies - I'm thinking they've "expired" while you were writing some code, easy mistake to make.

 

I see you're writing a game so you'll have a few scripts - I don't know what they are so I can't say what ones you need to check. Are they set when a user logs in?

Link to comment
Share on other sites

The issue is not the cookie but how you are setting it.

 

setcookie

 

Set it with all the values, domain (make sure it is either domain.com or www.domain.com) and a "/" for the directory. Doing that should set the cookie properly.

 

You are only setting the time, value and name, nothing else. This can cause issues on some/most servers.

Link to comment
Share on other sites

I'd prefer to use sessions instead - I've never touched cookies and don't know how to use them.

 

I would agree, but without him re-doing the script to use sessions instead, this should solve the problem until he decides to re-do it.

Link to comment
Share on other sites

Jiraiya, I started writing my 3rd online game

 

http://game.pictureinthesky.net

 

I never got around to finishing it but you're more than welcome to ALL the source to look at and play with.

 

I've just PM'd you with the admin logon information - not all the links in there work.

 

Don't be afraid to make new topics in the forums and click things.

Link to comment
Share on other sites

this is my login script is it set right?

 

 

/Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: player.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=http://narutotalesofthesannin.com/join.php>Click To Sign Up</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{

// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);

//then redirect them to the members area
header("Location: player.php");
}
}
}
else
{

// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?> 




</body>
</html>







Link to comment
Share on other sites

Add session_start() at the start of each script that uses them.

 

Here's a quick run-down seeing you're pressed for time.

 

Make & set a session var

$_SESSION['username']=$_POST['username'];

 

To read a session var

echo $_SESSION['username'];

 

When the user logs out:

$_SESSION=array();

 

This link has all the information you'll need:

http://uk3.php.net/session

Link to comment
Share on other sites

You would use them like you would any other variable.

 

Say you have a user that logs in - all you'd need to do is store their user ID number in a session variable.

$_SESSION['id']=$playeruserid;

 

Then for example, if you need to get all the information on them use something like this:

$usr=mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `userid`='".$_SESSION['id']."' LIMIT 1"));

Then you can use $usr like you would normally.

Link to comment
Share on other sites

Let's say in the "users" table you've got a field called "accesslevel" which says whether they're a standard player (1), help desk operator (2), moderator (3), head moderator (4) or admin (5). You can check in a small include file if their access level lets them access certain pages.

<?php
$usr=mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `userid`='".$_SESSION['id']."' LIMIT 1"));
if ($usr['accesslevel']<2) { //must be a member of staff
  header("Location: accessdenied.php");
  exit;
}
// if accesslevel is 2 or higher the script continues here

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.