hi, I'm fiddling around in php (my code is terrible btw) and I've managed to write a register/login script. This is probably a really dumb question, but it's been hurting my head for hours.
The way it works now is, say a user "killer" is logged in. There is a cookie that remembers killer when he returns, and every time he logs in or returns a page called killer.php is opened. I'm using $_GET... so in the address bar it would say index.php?page=killer.
However, if killer is already logged in and he attempts to access index.php, it doesn't redirect him to ?page=killer.
I can't figure out how to do this!
Take a look at this ugly code:
index.php:
<?php
session_start();
header("Cache-control: private");
include "connect.php";
if ( !isset($_COOKIE['owned']) && $_SESSION['user'] == "")
{
echo $_COOKIE['owned'];
?>
...html here...
<? }
else {
include($_GET['page']. '.php');
}
?>
login.php:
<?php
session_start();
header("Cache-control: private");
include "connect.php";
$user = $_POST['username'];
$pass = $_POST['password'];
$check_login=mysql_query("SELECT * FROM users WHERE user='$user' AND pass='$pass'");
if ( $row=mysql_fetch_array($check_login) ) {
$valid_user = 1;
}
else {
$valid_user = 0;
}
if (!($valid_user))
{
session_unset();
session_destroy();
header("Location: /matt");
exit();
}
else
{
$usern = $_SESSION['user'] = $row['user'];
$passwd = md5($pass);
$checked = $_POST['set'];
$time = time();
$cookie_data = $usern. "-" .$passwd;
$cookie_life = time() + 31536000;
if ($checked == "ON")
{
setcookie("owned", $cookie_data, $cookie_life);
header("Location: /matt/index.php?page=$usern");
}
}
mysql_close();
?>