Jump to content

Struggling with what seems an easy tutorial


mrreference

Recommended Posts

Hi guys, I am new to PHP and have been working on various tutorials with success, the latest one found here http://bhaviksblog.com/01/php-login-system-tutorial-part-1/ was to help me understand how to create a login script, with a profile page and upload/deleting images.

 

I have been stumped a bit as the script does take the users details and store them in the database, however when the user tries to login, it takes them to a page that informs the user they don't belong there.  Here is the website I have uploaded the data to so you can try and create a profile http://clientserver912.is-the-boss.com/index.php.

 

My index.php code is as follows:

 

<?php
session_start();

//Login form (index.php)

include "db_connect.php";
if(!$_POST['submit'])
{
?>
<html>
<b>Login</b>
<form method="post" action="index.php">
Username<input type="text" name="username" maxlength="16">
Password<input type="password" name="password" maxlength="16">
<input type="submit" name="submit" value="Login">
</form>
<a href="register.php">Register Here</a>
</html>

<?php
}
else
{
  $user = protect($_POST['username']);
  $pass = protect($_POST['password']);

if($user && $pass)
{
$pass = $pass; //compare the encrypted password
$sql="SELECT id,username FROM `users` WHERE `username`='$user' AND `password`='$pass'";
$query=mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($query) == 1)
    {
      $row = mysql_fetch_assoc($query); // mysql_fetch_assoc gets the value for each field in the row
      $_SESSION['id'] = $row['id']; //creates the first session var
      $_SESSION['username'] = $row['username']; // second session var

      echo "<script type=\"text/javascript\">window.location=\"home.php\"</script>";
    }
    else
   {
    echo "<script type=\"text/javascript\">
    alert(\"Username and password combination is incorrect!\");
    window.location=\"index.php\"</script>";
    }	
}
else
{			
    echo "<script type=\"text/javascript\">
    alert(\"You need to gimme a username AND password!!\");
    window.location=\"index.php\"</script>";
}
}
?>

 

And the code fot the page which indicates they don't belong here is:

 

<html>
<head><link rel="stylesheet" href="style.css"></head>

<div class="divider">

<?php
if($_SESSION['id'])
{
echo "<strong>Welcome ",$_SESSION['username'],"</strong>";

echo "<br/><a href=\"profilecp.php\">Edit Profile</a>";
echo "<br/><a href=\"profile.php?username=".$_SESSION['username']."\">View Profile</a>";
echo "<br/><a href=\"logout.php\">Logout</a>" ;
}
else
{
echo "You don't belong here!";
}
?>
</div>
</html>

 

If you have looked at the code on the tutorial site, you'll notice I have put \ before a ", I read this on another site as I was receiving this error

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /data/members/free/tripod/uk/c/l/i/clientserver912/htdocs/index.php  on line 39

 

and line 39 was

  echo "<script type="text/javascript">window.location="home.php"</script>";

 

If you can point me in the right direction that would be most appreciated, if not, are you aware of any tutorials that I can follow of the same nature.

 

regards

you need a session_start(); at the top of all of your pages else the session variables won't carry over

 

like this:

 

<?php session_start(); ?><html>
<head><link rel="stylesheet" href="style.css"></head>

<div class="divider">

<?php
if($_SESSION['id'])
{
echo "<strong>Welcome ",$_SESSION['username'],"</strong>";

echo "<br/><a href=\"profilecp.php\">Edit Profile</a>";
echo "<br/><a href=\"profile.php?username=".$_SESSION['username']."\">View Profile</a>";
echo "<br/><a href=\"logout.php\">Logout</a>" ;
}
else
{
echo "You don't belong here!";
}
?>
</div>
</html>

Thanks, I was salivating at the thought this script would work with such an easy line to add.  Unfortunately after adding it, it still went to the page saying "You don't belong here".

 

This is my updated code now as you suggested

<?php session_start();?>
<html>
<head><link rel="stylesheet" href="style.css"></head>

<div class="divider">

<?php
if($_SESSION['id'])
{
echo "<strong>Welcome ",$_SESSION['username'],"</strong>";

echo "<br/><a href=\"profilecp.php\">Edit Profile</a>";
echo "<br/><a href=\"profile.php?username=".$_SESSION['username']."\">View Profile</a>";
echo "<br/><a href=\"logout.php\">Logout</a>" ;
}
else
{
echo "You don't belong here!";
}
?>
</div>
</html>

this is the line of code that basically sets your "Logged In" state to TRUE.

 

$_SESSION['id'] = $row['id'];

 

if your login was correct, then it sets that variable.

 

if ( $_SESSION['id'] )

 

in plain english, the line above means "if true, then do whatever is in the following braces else do whatever is in the braces after the keyword 'else'"

 

so.... if your login was correct and you're still getting that error, it means that either you're having problems with your session variables not carrying over, or you don't have TRUE saved in your table under 'id'.

 

try this, change this line: $_SESSION['id'] = $row['id'];

 

to this: $_SESSION['id'] = TRUE;

 

and see what happens

 

 

 

I altered the code as you suggested and it continued as per usual "You do not belong here"

 

So I went to the database and changed the id DEFAULT to TRUE so the whole row now reads

 

Field  -  id

Type  -    int(10)   

Attribute  -  UNSIGNED 

Null  -  No

Default  -  (blank) but i did type TRUE in here and its not showing up now

Extra -  auto increment

 

Keyname  -  Primary

Unique  -  Yes

Fulltext  - no

 

Does this help?

 

I can give you the link to the database if you like.

sounds like you might be having problems with your sessions.  if you copy/pasted that code from a tutorial, I'm going to assume it's functional.  lets try this first:

 

on the login page, but this somewhere visible: echo session_id();

 

put that same line of code just above where it echo's out that you aren't supposed to be there like so:

 

else

{

echo session_id();

echo "You don't belong here!";

}

 

goto the login page, take note of the session ID, then log in, and on the page that says you don't belong there, see that the session ID is the same.  if not, there's your problem.

 

let us know what you get

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.