Jump to content

Things Aren't Working >_<


shadowayex

Recommended Posts

Ok, I'm trying to make a game site basically just to learn stuff and there's almost nothing to it. The reason is I can't get the site to recogize users. So far, this is what I have:index.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Register</title></head><body><a href="signup.php">Sign Up!</a><br /><a href="login.php">Login!</a></body></html>

signup.php

<?php$link = mysql_connect('localhost', 'cyldaw1_user', 'powerrewop')    or die('Could not connect: ' . mysql_error());mysql_select_db('cyldaw1_user') or die('Could not select database');$username = mysql_real_escape_string($_POST['username']);$password = sha1($_POST['password']);if ($username == "" || $password == ""){  echo "Please fill in all fields.";}else{ $check = mysql_query("SELECT * FROM players WHERE Username='$username'"); if (mysql_num_rows($check) == 0) {  $forward = mysql_query("INSERT INTO players (Username, Password) VALUES ('$username', '$password')");  $_SESSION['logged_in'] = true;  $_SESSION['user'] = $username; } else {   echo "Username is already registered. Please select another."; } if (isset($_SESSION['logged_in'])) {   header('location: home.php'); } mysql_free_result($check);}mysql_close($link);?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Register</title></head><body><div>Choose a Username and Password under 15 characters long.</div><form action="signup.php" method="post"><div>Desired Username<br /><input type="text" name="username" value="" /><br />Desired Password<br /><input type="password" name="password" value="" size="20" /><br /><input type="submit" name="submit" value="Sign Up!" size="20" /></div></form></body></html>

login.php

<?php$link = mysql_connect('localhost', 'cyldaw1_user', 'powerrewop')    or die('Could not connect: ' . mysql_error());mysql_select_db('cyldaw1_user') or die('Could not select database');$username = mysql_real_escape_string($_POST['username']);$password = sha1($_POST['password']);$result = mysql_query("SELECT * FROM players WHERE username='$username' AND password='$password'");if ($username == "" || $password == ""){   echo "";}else{  if (mysql_num_rows($result) == 1)  {     $_SESSION['logged_in'] = true;     $_SESSION['user'] = $username;     header('location: home.php');  }  else  {     echo "Error: Incorrect username or password!";  }}mysql_close($link);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Log In</title></head><body><form action="login.php" method="post"><div>Username: <input type="text" name="username" value="" size="20" /><br />Password: <input type="password" name="password" value="" size="20" /><br /><input type="submit" name="submit" value="Login" /></div></form></body></html>

home.php

<?php session_start();$user=$_SESSION['user'];if(isset($user)) echo "Welcome $user";?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>Home</title><style>table  {border-size: 4px;}</style></head><body></body></html>

The problem I'm having is once it goes to the home.php page, it does not display anything. What did I do wrong?

Link to comment
Share on other sites

Well, yeah but I don't want to make another page for logout. Can I make a link or button call a function, like the onclick option for JavaScript?
Again, it's the exact same thing you do with any other code. If you want to run a Javascript function to run the PHP code, then it needs to be an AJAX function which can call a PHP page to destroy the session. You can't run PHP code directly from Javascript.
Link to comment
Share on other sites

Well, yeah but I don't want to make another page for logout. Can I make a link or button call a function, like the onclick option for JavaScript?
You could destroy the session from the same page, just make sure there's some variable (in the query string) indicating you want to logout, like
index.php?action=logout

and link to that.

Link to comment
Share on other sites

Ok, i'm probably going to sound dumb, but i didn't really understand either of those answers. Ok, I don't want to use Javascript, I was wondering if there was a PHP equivalent to onclick, which I'm assuming there's not. Now, I was thinnking about using $_GET variable, but I don't think my idea would work. I was gonna use the action=logout idea, but I'm not sure how I'd work it. I thought about making an if statement that would try to match the action part to logout and if it's true, destroy the session, but I don't know how it'd work. I'm just a beginner at PHP and I honestly have picked up almost nothing from the tutorials because it doesn't explain how to work the destroy session to be called, only have it automatic. And of course, i don't want it automatic.Also, I was wondering if there's a PHP equivalent to the Javascript innerHTML function.

Link to comment
Share on other sites

I was wondering if there was a PHP equivalent to onclick, which I'm assuming there's not
PHP runs on the server, not on the browser. You don't capture client events like clicking on the server, only the browser captures those events.<a href="logout.php">That is how you run a PHP script on click. This is logout.php:
<?phpsession_start();session_destroy();header("Location: index.php");?>

I honestly have picked up almost nothing from the tutorials
The tutorials here are pretty limited. Check the official manual:http://www.php.net/manual/en/ref.session.phpRead the introduction parts, scroll down and read through the examples, and scan through the user comments to see what people are talking about.
Also, I was wondering if there's a PHP equivalent to the Javascript innerHTML function.
There is nothing in PHP that is equivalent to anything in Javascript, and vice versa, they are completely unrelated and are used for totally different purposes. They run in different environments (on the server vs. on the client) and at different times (PHP generates the page, Javascript is executed after the page is finished). If you want to tell what you're trying to do then we can tell you how to do it in PHP, but Javascript's innerHTML property is a property of elements in the DOM that Javascript has access to, and PHP doesn't really have a need to access the DOM of the page it's creating, it doesn't need to know anything about it in fact.
Link to comment
Share on other sites

You can tell PHP to write something anywhere.

<?php$message = "some message";?><html>  <head>	<title><?php echo $message; ?></title>	<script type="text/javascript">	alert("<?php echo $message; ?>");	</script>  </head>  <body>	<div><?php echo $message; ?></div>  </body></html>

Link to comment
Share on other sites

Oh, duh. I feel dumb :). Ok, do you know anything about making equations for games. Like controling damage and nd defense and all that stuff? Because honestly I don't know what to do about that. I suppose I could try things.

Link to comment
Share on other sites

Oh, duh. I feel dumb :) . Ok, do you know anything about making equations for games. Like controling damage and nd defense and all that stuff? Because honestly I don't know what to do about that. I suppose I could try things.
I would buy a book on writing games. You might also consider working in flash. Javascript animation is cute for little stuff, but hardly for games.
Link to comment
Share on other sites

I was thinking of different equations I learned from Alg 1 and 2 and stuff. I know I'd use Exponential Growth curve for the Experience equations. I don't know much else. But I'm trying to figure out how I'd even load the battle function. I suppose I'd have to put the battle code in a new page and then have a link to it huh? And if so, can somehow import the code into my home.php page? Or would I need to just have a link back to home from the battle page?And I wanted to use flash, but I don't have money to buy software for it. I'm using a free hosting site for this.

Link to comment
Share on other sites

If you use PHP only it's not going to be a very user-friendly game, you're going to reload the page to do anything. If you're using Javascript then it's not going to be secure, anyone would be able to write scripts to cheat. It would definately be best to use Flash, you don't necessarily need to pay for it. There are Flash authoring programs for Linux, for example.

Link to comment
Share on other sites

Well, it might depend on your plans for the game. If you're thinking single-shooter, constant action, yeah, it's going to be a drag.But if it's more of a strategy game, like Chess or Nethack or even Battleship, then you could do it with PHP and AJAX and slow animation might not be a big deal. AND it would still be a great learning experience because you'd be working with many different aspects of "Webbing." Even better if you used your server's file system to make it a multi-player game on multiple computers.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...