Código PHP:
$query = "SELECT * FROM online ORDER BY username";
$cad = mysql_db_query($dbname,$query ) or die (mysql_error());
while($row = mysql_fetch_array($cad)){
if ($row[username] == $username){
}else{
$sql = "INSERT INTO online (username, lastlogin, ip) VALUES ('$username','$lastlogin', '$ip_real')";
$result = mysql_query($sql);
}
}
Pero lo que sucede es que no me inserta en la tabla el usuario sin embargo sin el if lo inserta pero si se actualiza la pagina lo inserta tantas veces como actualices la pagina y da error en las estadisticas.
codigo completo:
Código PHP:
<?
include("includes/connect.php");
include('languajes/english/register.php');
if ($_POST['username']) {
//Comprobacion del envio del nombre de usuario y password
$username=$_POST['username'];
$password=$_POST['password'];
if ($password==NULL) {
echo "La password no fue enviada";
}else{
$query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($query);
if($data['password'] != $password) {
echo "Wrong username/password";
}else{
$query = mysql_query("SELECT username,password FROM users WHERE username = '$username'") or die(mysql_error());
$row = mysql_fetch_array($query);
$_SESSION["s_username"] = $row['username'];
$_SESSION["s_lastlogin"] = $row['lastlogin'];
if ($_SERVER) {
if ( $_SERVER[HTTP_X_FORWARDED_FOR] ) {
$ip_real = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif ( $_SERVER['HTTP_CLIENT_IP'] ) {
$ip_real = $_SERVER['HTTP_CLIENT_IP'];
}
else {
$ip_real = $_SERVER['REMOTE_ADDR'];
}
}
else {
if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
$ip_real = getenv( 'HTTP_X_FORWARDED_FOR' );
}
elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
$ip_real = getenv( 'HTTP_CLIENT_IP' );
}
else {
$ip_real = getenv( 'REMOTE_ADDR' );
}
}
$username=($_SESSION['s_username']);
$lastlogin=date("d/m/Y h:i A");
$query = "SELECT * FROM online ORDER BY username";
$cad = mysql_db_query($dbname,$query ) or die (mysql_error());
while($row = mysql_fetch_array($cad)){
if ($row[username] == $username){
}else{
$sql = "INSERT INTO online (username, lastlogin, ip) VALUES ('$username','$lastlogin', '$ip_real')";
$result = mysql_query($sql);
}
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="styles/crystal.css" rel="stylesheet" type="text/css" />
</head>
<?php
if (isset($_SESSION['s_username'])) {
echo ('
<body class="body">
<div class="div_container">
<div class="div_title">Log In</div>
');
echo ('<div class="div_text">Welcome
');
echo ($_SESSION['s_username']);
echo ('
<br />
<a href="logout.php">Logout</a>
</div>
</div>
<br />
</body>
');
}
else
{
echo('
<body class="body">
<form method="POST">
<div class="div_container">
<div class="div_title">Log In</div>
<div class="div_text">
Username:
<br />
<input name="username" type="text" />
<br />
<br />
Password:
<br />
<input name="password" type="password" />
<br />
<br />
<input type="submit" name="Submit" value="'
);
echo $lang_register["Submit"];
echo('
" />
</div>
<div class="div_text">
<a href="register.php">Register</a>
</div>
</form>
</body>
');
exit;
}
?>
</html>