简体   繁体   中英

parse error in log in php

I am building a site which has a user database where the users can log in and sign up. When I test the PHP code for login, it says “parse error” once and invalid { in line 16 . What is worng in my code?

<?php

$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];
$con = mysql_connect("localhost","root","xxxxxxxxxx") or die('Could not connect:'.mysql_error());
$test = mysql_select_db("users",$con)or die("unable to connect");

$result = "SELECT  * FROM users where username='$user' AND password='$pass'";
$alpha = @mysql_query($result,$con);

if($alpha==1)
{
     $expire=time()+60*60*24*30;
     setcookie("id",$row['id'],$expire);
     echo "Logged in. as <b>".$row['username']."</b>";
     $userID = $row['id'];
     header("index map.html")
 }
 else
 {
     echo "<b>Username or password  is wrong</b><br><br>";
     header("index.html")
 }

 mysql_close($con);

 ?>

There are a couple of issues with your PHP here.

  1. header needs a semi-colon closing it
  2. header needs a location set.. ie header('location: index.html');

you should replace

header("index map.html")

with

header("location: map.html");

Also you need to replace

header("index.html")

with

header("Location: index.html");

What the hell ? Where is row comes from ? you didnt call any mysql_fecth_array or some thing related to traverse the result int a row .. at least u code have used

$row = mysql_fetch_result/array($result,$conn);

then use row as my suggestion ...

if($alpha==1)
{
     $expire=time()+60*60*24*30;
     setcookie("id",$row['id'],$expire);
     echo "Logged in. as <b>".$row['username']."</b>";
     $userID = $row['id'];
     header("index map.html")
 }

first of all your code is very unsecure ! We can easily do some SQL Injection. Try to use at least: mysql_real_escape . ( How to use mysql_real_escape_string function in PHP ). Also using PDO will be better (in my point of view).

The you forgot the dots in

$result = "SELECT * FROM users WHERE username='.$user.' AND password='.$pass.'"; (line 7)

And you forgot to put the semi-colons and the field keyname (line 21) should be header("location: index.html");

Best regards.

您的header()函数调用时错误地将其更正

header("location:index-map.html");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM