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!
<?php
//login page
session_start();
session_register("username", "id", "gid");
require ("config.php");
//if entered blank form returns error
foreach ($_POST as $field => $value)
{
if ($value == "")
{
$blanks[] = $field;
}
}
if (isset ($blanks) )
{
$message = "Following fields are blank. Please enter the required information: ";
foreach ($blanks as $value)
{
$message .= "$value, ";
}
extract ($_POST);
include ('login_form.php');
exit();
}
//Define variables.
$username = trim($_POST['username']);
$password = trim($_POST['password']);
//time to select information
$sql ="SELECT `username`,`password`, `id`, 'gid' FROM members WHERE username = '$username' AND password = md5('$password')";
$result = mysql_query($sql, $con);
$row = mysql_fetch_assoc($result);
mysql_error();
//We check if username and password is correct
$num = mysql_numrows ($result);
if ($num != 0)
{
print "Welcome $username - your log-in succeeded!";
}
else
{
print "Wrong username or password";
}
?>
Here is the page where I want the sessions to appear. Now would I need to start a query to receive the users id and if I did how would I accomplish this?
So basically, if the session wasn't registered, they'd go back to the main page. All I know is you gotta put that above PHP on every page you've got requiring a login.
// insert this on all pages where the users must be logged in
session_start();
if(!$_SESSION['logged_in']) {
header('Location: http://www.mysite.com/main_login.php');
}