Jump to content

Recommended Posts

I just started using php and i figured what better to practice on then creating a ficticious login script. Im using easyphp and here is the code.

 

<?php

$username="php";

$password="ironpass";

$database="userlist";

$table="login";

$connectmysql="mysql_connect(localhost,$username,$password)";

 

if (!isset($_POST['loggedin']))

  {

  echo "<a>please log in</a><br><form method='post'><br>Username:<input type='text' name='username'><br>Password:<input type='text' name='password'><br><input type='submit' name='loggedin' value='Log In'><a href='resister.php'>Register here</a>";

  }

 

elseif (isset($_POST['loggedin']))

  {

  $user="$_POST['username']";

  $userpass = "$_POST['password']";

  $query1="SELECT*FROM login";

  $query2="SELECT*FROM login WHERE username='$user'";

  $query3="SELECT*FROM login WHERE username='$userpass'";

  $result1="mysql_query($query1)";

  $result2="mysql_query($query2)";

  $result3="mysql_query($query3)";

 

 

  $connectmysql

  mysql_select_db($database) or die("unable to select database");

 

  if($user == $result2)

    {

      if($userpass == $result3)

      {

      echo "Thank you for logging in";

      setcookie ["$user ","$userpass "];

      }

    }

  }

 

 

elseif($_COOKIE['user'] == $userpass)

  {

  echo "Welcome $user Enjoy the site";

  }

  ?>

 

 

on line 15 which is   $user="$_POST['username']"; I get this error Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\easyphp1-8\www\website temp\index.php on line 15

 

 

its probably some stupid thing i did or the way i wrote it... idk if i can put variables there, but i get the same error when its at the top with the other variables... so im stumped, also if line 15 is removed it gets the same error with the next two variables below it, idk beyond those two cause i stopped checking em. any help is appreciated thanks in advance.

 

 

btw please dont comment and tell me that thats an insecure way of making a login script becasue i dont care, im just practicing.

 

 

Link to comment
https://forums.phpfreaks.com/topic/67396-solved-login-script-doesnt-work/
Share on other sites

Try:

 


<?php
$username="php";
$password="ironpass";
$database="userlist";
$table="login";
$connectmysql="mysql_connect(localhost,$username,$password)";
  
if (!isset($_POST['loggedin']))
  {
  echo "<a>please log in[/url]
<form method='post'>
Username:<input type='text' name='username'>
Password:<input type='text' name='password'>
<input type='submit' name='loggedin' value='Log In'><a href='resister.php'>Register here[/url]";
  }elseif{
  (isset($_POST['loggedin']))
  
  $user="$_POST['username']";
  $userpass = "$_POST['password']";
  $query1="SELECT*FROM login"; 
  $query2="SELECT*FROM login WHERE username='$user'";
  $query3="SELECT*FROM login WHERE username='$userpass'";
  $result1="mysql_query($query1)";
  $result2="mysql_query($query2)";
  $result3="mysql_query($query3)";
  
  
  $connectmysql
  mysql_select_db($database) or die("unable to select database");
  
  if($user == $query2)
    {
      if($userpass == $query3)
      {
      echo "Thank you for logging in";
      setcookie ["$user ","$userpass "];
      }
    } 
  }
  

elseif($_COOKIE['user'] == $userpass)
  {
  echo "Welcome $user Enjoy the site";
  }
  
  ?>

ok, ill try could you explain to me what you changed though?

 

 

EDIT: i tried it, i belive where you put

}elseif{
  (isset($_POST['loggedin']))

you ment

}elseif
  (isset($_POST['loggedin']))
   {

? anyways i still get the same error with the new programming.

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\easyphp1-8\www\website temp\index2.php on line 18

only its on line 18 because you format your code differently then i do line 18 is still

$user="$_POST['username']";

change:

}elseif{
  (isset($_POST['loggedin']))

 

to

} elseif(isset($_POST['loggedin'])){

 

simple elseif syntax:

if($a == 1){
   echo 1;
} elseif($a == 2){
   echo 2;
} else{
   echo 3;
}

 

He probably wrote it in a rush and did that little error.

yea thats probably what happened, but i still have the problem, i realized that my code wouldnt work as mentioned above, just because things were out of order. my new code is

 

<?php
$username="php";
$password="ironpass";
$database="userlist";
$table="login";
$connectmysql="mysql_connect(localhost,$username,$password)";
$selectdb="mysql_select_database($database) or die('unable to select database')";

if(isset($_COOKIE['user']))
  {
    if ($_COOKIE['user'] == $userpass)
    {
  echo "Welcome $user Enjoy the site";
    }
  }
  
elseif (!isset($_POST['loggedin']))
  {
  echo "<a>please log in</a><br><form method='post'><br>Username:<input type='text' name='username'><br>Password:<input type='text' name='password'><br><input type='submit' name='loggedin' value='Log In'><a href='resister.php'>Register here</a>";
  }

elseif (isset($_POST['loggedin']))
  {
  
  $user="$_POST['username']";
  $userpass = "$_POST['password']";
  $query1="SELECT*FROM login"; 
  $query2="SELECT*FROM login WHERE username='$user'";
  $query3="SELECT*FROM login WHERE username='$userpass'";
  $result1="mysql_query($query1)";
  $result2="mysql_query($query2)";
  $result3="mysql_query($query3)";
  
  
  $connectmysql
  @mysql_select_database($database) or die('unable to select database');
  
  if($user==$result2)
    {
      if($userpass == $result3)
      {
      echo "Thank you for logging in";
      setcookie ["$user ","$userpass "];
      }
    } 
  }
  ?>

 

but i still have the original problem :( ive heard that easy php could be the problem... so if someone could run it on thier machine and tell me if it works that would be nice... unless someone can point out my error, i would prefer that, thanks again! also if someone could help me out with the connecting to the database please, im just looking at the line

  @mysql_select_database($database) or die('unable to select database');

 

and it looks just wrong. i would appreciate if someone could set me straight on that, thanks

Please use [code] tags. Try this:

<?php
$username="php";
$password="ironpass";
$database="userlist";
$table="login";
$connectmysql=mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("unable to select database");

if (!isset($_POST['loggedin']))
  {
  echo "<a>please log in[/url]
<form method='post'>
Username:<input type='text' name='username'>
Password:<input type='text' name='password'>
<input type='submit' name='loggedin' value='Log In'><a href='resister.php'>Register here[/url]";
  }

elseif (isset($_POST['loggedin']))
  {
  $user=$_POST['username'];
  $userpass = $_POST['password'];
  $query1="SELECT*FROM login";
  $query2="SELECT*FROM login WHERE username='$user'";
  $query3="SELECT*FROM login WHERE username='$userpass'";
  $result1=mysql_query($query1);
  $result2=mysql_query($query2);
  $result3=mysql_query($query3);

  /*if($user == $result2)
    {
      if($userpass == $result3)
      {
      echo "Thank you for logging in";
      setcookie ["$user ","$userpass "];
      }
    }
  }


elseif($_COOKIE['user'] == $userpass)
  {
  echo "Welcome $user Enjoy the site";
  }*/
  ?>

I commented out the other areas because they don't make sense to me. But at least you won't get that error you posted.

yea sorry i couldnt remember wwaht the tag was for code  ??? which makes me an idiot... but i figured it out eventually. and for the bottom part of the code its checking to see if the cookie placed on there computer (which contains thier password) matches the password stored in mysql. i redid the code and placed it at the top of the page because i realized the site would never execute that code because it would make you relogin everypage. so that code was moved to the top and the other code was placed below it. it might not make sence because im new and maybe its just a messed up crappy idea... idk... anyways thanks for the help.

echo "Thank you for logging in";
      setcookie ["$user ","$userpass "];

elseif($_COOKIE['$user'] == $userpass)
  {
  echo "Welcome $user Enjoy the site";
  }*/
  ?>

 

 

wouldnt that translate into

$userpass == $userpass

?

Yep, that was what I fixed. Anyways, here, just use this code. BTW, you don't know how to use setcookie() either. Read up on that. Try this, it should work with a few fixes. I put them in comments.

<?php
$username="php";
$password="ironpass";
$database="userlist";
$table="login";

mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die("unable to select database");

if (!$_POST['loggedin'] && !$_COOKIE['user'])
{
echo "<a>please log in[/url]
	<form method='post'>
	Username:<input type='text' name='username'>
	Password:<input type='text' name='password'>
	<input type='submit' name='loggedin' value='Log In'><a href='resister.php'>Register here[/url]";
}

else if ($_POST['loggedin'] && !$_COOKIE['user'])
{
$user = $_POST['username'];
$pass = $_POST['password'];
// grab the database table & all its columns
$query = mysql_query("SELECT*FROM login WHERE username='$user'") or die(mysql_error());
$result = mysql_fetch_assoc($query) or die(mysql_error());

// checks to see if the username is in the database
if (!mysql_num_rows($query)) echo "You're not registered.";
else if($user == $result['username']) /* replace the 'username' part with the column for username in the database */
{
	if($pass == $result['password']) /* replace the 'password' part with the column for password in the database */
	{
		echo "Thank you for logging in";
		setcookie($user);  /* user parentheses not brackets for cookies  And don't include password - not safe */
	}
	else echo "Incorrect password";
}
}


else if($_COOKIE['user']) // you can't call variables that are in different clauses
{
echo "Welcome {$_COOKIE['user']}. Enjoy the site";
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.