Computer Science Canada

Saving PHP Data

Author:  TheLastFall [ Thu Oct 26, 2006 6:55 pm ]
Post subject:  Saving PHP Data

Okay, obviously I need help. I need to know how to save a Username and Password and I need to be able to read it and see it it is correct. I'm probably going to get flamed for this topic. I've just recently started to learn PHP so I'm new to it. I have learned how to get a name and password but not how to save it.
So far this is how I get the Username and Password:

code:

<form action="index.php" method=post>

Username:
 <br> <input type="text" name="Username">

 <p> Password:
 <br> <input type="text" pass="Password">
 <p>


 <input type="submit" name="submit" value="Submit">
 </form>


Am I going to be needing arrays in this because I've read them over and I do not understand how to do anything.
This is what my arrays look like:
code:

<?php

$account = array (
                array ( "name" => "Username",
                                "pass" => "Password");
                                       
print $account[0]["name"];
print ("<br>");
print $account[3]["pass"];

?>

I have a feeling I'm going to get flamed a lot and probably not even by moderators. But I need a lot of help with this, I'm pathetic I know. Could someone please help me? If you send links to tutorials I'll probably mess it up more than my array. There is a chance they might help... Well could someone just respond?

Author:  Craige [ Thu Oct 26, 2006 7:13 pm ]
Post subject: 

You wont get flamed. It's just a question, and a legitimate one at that. I remember when I was new to PHP. It was all questions, all the time.

Now, you have a couple options when it comes to saving user data. The most common, expecually in this type of purpose, is a database. Databases allow your to store data in an organized way, and retrieve it at lightning fast speeds. Take a look around php.net's documentation on the MySQLi extension. Ignore any reference to things called objects, classes, and in general the term OOP. You won't really be able to use it yet.

Specifically within this extension, you will want to look at mysqli_connect(), mysqli_query(), mysqli_fetch_array(), and mysqli_close(). You need to set up a MySQL database before you can use any of these though. If you are paying for a host, I can almost guarantee that they have MySQL databases. Contact them for details on how to set them up.

[i]Remember, with that documentation, only pay attention to the procedural style, not the object oriented style.

Your second option, is to use flat files, and the filesystem library. It isn't that common of a method for things like this, but it is available. Just take a look around that page and see what you can find. If you choose to use this method (not recommended by myself), and can't figure out how to do things, post back here and I can show you some examples.

Author:  r.3volved [ Thu Oct 26, 2006 7:23 pm ]
Post subject: 

^^ great post man

...and from St. Thomas, never would have thought...
Cool

Author:  octopi [ Fri Oct 27, 2006 4:50 pm ]
Post subject: 

THe below method just loads the usernames and passwords into an array, then it goes threw the array and sees if the username passed via the browser is in the array. If it finds it, then it displays the message "Hello Jeb" where Jeb is the username entered. If not, it gives you an error message saying the combination is invalid.

If you need and help with this, or clarification let me know.

code:

<?php
$cred=array();
$cred[]=array('username'=>'Jeb',   'password'=>'jebspass');
$cred[]=array('username'=>'Vince', 'password'=>'Aquaman');
$cred[]=array('username'=>'Eric',  'password'=>'Maserati');
$cred[]=array('username'=>'Turtle','password'=>'Fujiwara');
$cred[]=array('username'=>'Drama', 'password'=>'loser');

$username=$_REQUEST['Username'];
$password=$_REQUEST['Password'];

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success) {
  print "Hello $username";
}
else {
  print "Your username/password combination is invalid.";
}

?>


range(0,count($cred) - 1) returns an array containing 0,1,2,3,4...all the way to original array length -1

$cred[] just adds a new element to the existing array. you could write this as $cred[count($cred)]

Author:  TheLastFall [ Fri Oct 27, 2006 5:31 pm ]
Post subject: 

Okay, Octopi I've tried your code and I have a slight problem with it. Okay, say I want to have one page that you enter your username and password and if its right you want it to go to another page and if it's wrong you want it to say "Invalid username/password" above the check box.
How could I mod this so it would be like that.
code:

<form method=post>
<p>Username:
<br> <input type="text" name="username">
<p> Password:
<br> <input type="text" name="password">
<p>
<input type="submit" name="submit" value="Login">
</form>

<?php
$cred=array();
$cred[]=array('username'=>'TheLastFall',   'password'=>'1234');
$cred[]=array('username'=>'Magiangel', 'password'=>'5678');

$username=$_REQUEST['Username'];
$password=$_REQUEST['Password'];

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success = 1) {
  <a href='overview.php'>
}
else {
  print "Invalid username/password.";
}

?>

And there is a something wrong with the one squiggly bracket. Where it says.
code:

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}  <----- That one.

I don't follow why it is a problem.

Author:  octopi [ Fri Oct 27, 2006 7:21 pm ]
Post subject: 

code:
<form method=post>
<p>Username:
<br> <input type="text" name="username">
<p> Password:
<br> <input type="text" name="password">
<p>
<input type="submit" name="submit" value="Login">
</form>

<?php
$cred=array();
$cred[]=array('username'=>'TheLastFall',   'password'=>'1234');
$cred[]=array('username'=>'Magiangel', 'password'=>'5678');

$username=$_REQUEST['username'];
$password=$_REQUEST['password'];

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success == 1) {
  print "<a href='overview.php'>";
}
else {
  print "Invalid username/password.";
}

?>


There were a number of problems....
You changed the name of the form field. this must match the names specified by $_REQUEST.

You also had if($success = 1), this assigns a value of 1 to success, and will always be true.

finally you were missing print ""; around your link.

Here is a version that automatically redirects the browser.
code:
<?php

$username=$_REQUEST['username'];
$password=$_REQUEST['password'];

if(!$username) { ?>

<form method=post>
<p>Username:
<br> <input type="text" name="username">
<p> Password:
<br> <input type="text" name="password">
<p>
<input type="submit" name="submit" value="Login">
</form>

<?
//above displays form and exits if there is no username.
exit(0);
}

$cred=array();
$cred[]=array('username'=>'TheLastFall',   'password'=>'1234');
$cred[]=array('username'=>'Magiangel', 'password'=>'5678');
$cred[]=array('username'=>'jeb', 'password'=>'pass');

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success) {
  header("Location: overview.php");
}
else {
  print "Invalid username/password.";
}

?>


the line header("Location: overview.php");

tells the users browser to redirect to the overview.php page.
this wont work if youve already printed out some content.

Author:  TheLastFall [ Fri Oct 27, 2006 7:51 pm ]
Post subject: 

I understand it now, all except for one part. I do not understand the Header part. Can you please clarify it for me?

Author:  TheLastFall [ Fri Oct 27, 2006 8:10 pm ]
Post subject: 

When it does it, it seems like it sends the data but it sends it more then once so it reaches an error saying header already sent. Is that what you meant at the end of your post? I don't really understand what that means.

Author:  octopi [ Fri Oct 27, 2006 8:37 pm ]
Post subject: 

The header is a special peice of data sent to a browser that tells it what the content is about, how it should be displayed, and other things.

If you output something BEFORE you send the header it will be too late.

Make sure you don't have any print statements before the header. Or, that nothing is being outputted at all before it. Let me see your code if you need more help.

Author:  TheLastFall [ Fri Oct 27, 2006 8:43 pm ]
Post subject: 

I do think I understand but just so I don't mess anything up.
This is the login page:
code:

<html>
<style type=text/css>
body {
font-family: serif;
color:#FFFFFF;
background-color:#000000;
}
img {
border-style:solid;
border-color:#444444;
border-width:1px;
}
table {
color:#FFFFFF;
background-color:transparent;
border-style:solid;
border-color:#444444;
border-width:1px;
}
a {
color:#FFFFFF;
text-decoration:none;
}
a:hover{
color:#FFFFFF;
text-decoration:blink;
background-color:#222222;
}
</style>

<title>Tactical Force</title>
<body>
<center><div><img src ='tactical_force.jpg'></div></center>

<table width=10%>
<tr>
<td>
<a href=index.php>Home</a>
</td>
</tr>
<tr>
<td>
<a href=login.php>Login</a>
</td>
</tr>
<tr>
<td>
<a href=error.php>Register</a>
</td>
</tr>
<tr>
<td>

</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>

<?php

$username=$_REQUEST['username'];
$password=$_REQUEST['password'];

if(!$username) { ?>

<form>
<p>Username:
<br> <input type="text" name="username">
<p> Password:
<br> <input type="text" name="password">
<p>
<input type="submit" name="submit" value="Login">
</form>


<?
exit(0);
}

$cred=array();
$cred[]=array('username'=>'TheLastFall',   'password'=>'1234');
$cred[]=array('username'=>'Magiangel', 'password'=>'5678');
$cred[]=array('username'=>'jeb', 'password'=>'pass');

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success) {
  header("Location: overview.php");
}
else {
  print "Invalid username/password.";
}

?>

</body>
</html>


And this is the Overview page:
code:

<html>
<style type=text/css>
body {
font-family: serif;
color:FFFFFF;
background-color:#000000;
}
img {
border-style:solid;
border-color:444444;
border-width:1px;
}
table {
color:FFFFFF;
background-color:transparent;
border-style:solid;
border-color:444444;
border-width:1px;
}
a {
color:FFFFFF;
text-decoration:none;
}
a:hover{
color:FFFFFF;
text-decoration:blink;
background-color:222222;
}

</style>
<body>



</body>
</html>

I probably have a lot of programming errors in that, I don't know I have really read it over. Well thats the code I have.

Author:  octopi [ Sat Oct 28, 2006 1:42 pm ]
Post subject: 

No, your not really understanding....

All the html code up to <?php will be outputted right away.
So that means there is already some output.

In this situtation it is best to just change the header line to a link like you had, and make the user click threw.



code:
print "<a href='overview.php'>Continue</a>";

Author:  Craige [ Sun Oct 29, 2006 9:26 pm ]
Post subject: 

Okay, it seems you didn't like my advice, and that's okay, just know something about this method: when you have user names hard coded into a script, it becomes insecure, and not flexable either. The reason is:

a) if a user downloads your script, or in anyway views the php source, you usernames and passwords are revealed.

b) You should check the username and password at the top of every secure page, to make sure the user hasn't just gotten a hold of the url. With this method, you will have to put that array in an included file so it can be global to the entire site, and not static to every page.

Like I said, it's fine to do it like this for now, but when you start to learn more about PHP, you will want to change it.

Now, about you header problem: put this at the very top of you page:

<?php ob_start() ?>

The reason you need to do this (the way your coding) is that headers have to be the very first thing sent to the browser. They can not be sent after anything else. What that line I gave you does, is turn on output buffering, which holds all output in a buffer in memory untill the script finishes. With this, you can send headers at any time, as nothing will have been sent to the browser yet, and when the output buffer sees a header, it will just push it to the top of the buffer so it becomes the first thing sent to the browser.

Note: If you want to send the buffer to the browser early, call ob_flush(). This will send all data to the browser. You don't have to worry about calling this at the end of the script, as PHP will automatically flush the buffer when preforming garbage cleanup (freeing memory, resourses, and such)

Author:  octopi [ Mon Oct 30, 2006 12:00 am ]
Post subject: 

Craige is right this is not a good method for making a secure page, but I assumed this was just something to help you learn. As such mysql may be a little advanced for you just yet, so I suggested an alternative.

For improved security you shouldn't place the passwords in plain text, instead you should hash them, and store that value. You would then hash the incoming password to be checked, and then compare the stored hash, with the hash generated from the password to be checked. The same input will always result in the same hash. Hashes are a one-way system, which means it is not possible to reverse. You can not find the password if you are given just the hash. You can however compare them.

Author:  TheLastFall [ Tue Oct 31, 2006 4:14 pm ]
Post subject: 

I know I haven't replied in a while but I know what you mean about encrypting the file, I'll look into that later but right now I'm having trouble with placing the login information into the coding and having it in a specific spot. I doubt this will work if any of you try it but the coding is:
code:

<head>
<title>Tainted Generation</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<link rel="alternate" type="application/rss+xml" title="My RSS Feed" href="rss.xml" />
</head>

<body>

<div id="logo">
  <div id="sitename">Tainted Generation</div>
  <div id="pagetitle">
    <h1>A War Worth Fighting...</h1>
  </div>
</div>
<div id="navigation">
  <div class="blocktitle"><h2>Navigation</h2></div>
  <div class="blockcontent">
    <ul class="nav">
      <li class="nav"><a href="index.php" title="Home">Home</a></li>
<li class="nav"><a href="login.php" title="Login">Login</a></li>
<li class="nav"><a href="register.php" title="Register">Register</a></li>
<li class="nav"><a href="progress.php" title="Progress">Progress</a></li>
<li class="nav"><a href="faq.php" title="Frequently Asked Questions">F.A.Q.</a></li>
</ul>


  </div>


  <div class="blocktitle"><h2>A New Generation</h2></div>
  <div class="blockcontent">The world is in chaos and there are a few soldiers who can stop it, become the most powerful soldier and prevent the chaos from spreading or help it expand.</div>
 

  </div>
</div>

<div id="main-body">
  <div class="content">
    <div class="blocktitle"><h2>Login</h2></div>
    <div class="blockcontent"></div>

<?php

$username=$_REQUEST['username'];
$password=$_REQUEST['password'];

if(!$username) { ?>

<?
exit(0);
}

$cred=array();
$cred[]=array('username'=>'TheLastFall', 'password'=>'1234');
$cred[]=array('username'=>'Magiangel', 'password'=>'5678');

$success=0;

foreach (range(0,count($cred) - 1) as $k) {
  if($username == $cred[$k]['username']) {
    if($password == $cred[$k]['password']) {
      $success = 1;
      break;
    }
  }
}
if($success) {
print "Welcome $username, to Tainted Generation";
}
else {
  print "Invalid Account Info.";
}?>

<div class="spotlight-left">
        <div class="blocktitle"><h2>Tips</h2></div>
        <div class="blockcontent">If you are experiancing any difficulties with your account or loging in please send us a message, which could be found in the contact us link.</div>
      <br>
</div>

<hr>

</body>
</html>

I'm just wondering where on that I can put it so that it is inside the first block title class. I've tried it numerous ways but the login won't appear in that section, I can only have it appear in another place but none of the content will appear until the person has entered the username and password. I don't know if this is specific enough.

Author:  Craige [ Wed Nov 01, 2006 7:23 pm ]
Post subject: 

I'm sorry, I don't exactly understand your question.

Author:  TheLastFall [ Wed Nov 01, 2006 8:54 pm ]
Post subject: 

Thats ok I figured it out.

Author:  Craige [ Wed Nov 01, 2006 8:55 pm ]
Post subject: 

That's good. Congratulations.


: