PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Worqy
Forum Newbie
Posts: 20 Joined: Tue Feb 02, 2010 6:41 am
Post
by Worqy » Tue Feb 02, 2010 6:44 am
Hello.
I've made my first login script in php.
I use this code to login:
Code: Select all
<?php
session_start();
// Check if he wants to login:
if (!empty($_POST[username]))
{
require_once("connect.php");
// Check if he has the right info.
$query = mysql_query("SELECT * FROM members
WHERE username = '$_POST[username]'
AND password = '$_POST[password]'")
or die ("Error - Couldn't login user.");
$row = mysql_fetch_array($query)
or die ("Error - Couldn't login user.");
if (!empty($row[username])) // he got it.
{
$_SESSION[username] = $row[username];
echo "Welcome $_POST[username]! You've been successfully logged in.";
exit();
}
else // bad info.
{
echo "Error - Couldn't login useasr.<p>
Please try again.";
exit();
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h5>Login</h5></td>
</tr>
<tr>
<td width="100%"><label>Username: <input type="text" name="username" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>
</body>
</html>
But I have some problems creating a code to logout with.
klevis miho
Forum Contributor
Posts: 413 Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:
Post
by klevis miho » Tue Feb 02, 2010 7:36 am
use a session_destroy()
Paws
Forum Newbie
Posts: 15 Joined: Sun Nov 30, 2008 8:26 am
Post
by Paws » Tue Feb 02, 2010 7:55 am
Code: Select all
<?php
session_start();
//Unset the information stored in the session before destroying it
session_unset();
session_destroy();
//Redirect user
header("Location: index.php");
?>
Worqy
Forum Newbie
Posts: 20 Joined: Tue Feb 02, 2010 6:41 am
Post
by Worqy » Wed Feb 03, 2010 11:15 am
Thank you.
Now I also have some problems at my register.php file.
Code: Select all
<?php
// Check if he wants to register:
if (!empty($_POST[username]))
{
// Check if passwords match.
if ($_POST[password] != $_POST[password2])
exit("Error - Passwords don't match. Please go back and try again.");
// Assign some variables.
$date = mktime("d - m - Y");
$ip = $_SERVER[REMOTE_ADDR];
require_once("connect.php");
// Register him.
$query = mysql_query("INSERT INTO members
(username, firstname, lastname, password, date, ip)
VALUES ('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[password]','$date','$ip')")
or die ("Error - Couldn't register user.");
echo "Welcome $_POST[username]! You've been successfully reigstered!<br /><br />
Please login <a href='login.php'><b>here</b></a>.";
exit();
}
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="register.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h5>Registration</h5></td>
</tr>
<tr>
<td width="100%"><label>Desired Username: <input type="text" name="username" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>First Name: <input type="text" name="firstname" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Last Name: <input type="text" name="lastname" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Register!"></td>
</tr>
</table>
</form>
</body>
</html>
When I try to register it said:
Warning: mktime() expects parameter 1 to be long, string given in C:\xampp\htdocs\Game\register.php on line 11
Error - Couldn't register user.
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Wed Feb 03, 2010 11:20 am
You want date(), not mktime().
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Worqy
Forum Newbie
Posts: 20 Joined: Tue Feb 02, 2010 6:41 am
Post
by Worqy » Thu Feb 04, 2010 6:35 am
AbraCadaver wrote: You want date(), not mktime().
Thank you. But I still get the "Error - Couldn't register user." Message