Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a website and I have some HTML code and PHP code in a file like this

<html>
    some code
<?php
function login()
{
if (empty ($_POST['username']))
{
    return false;
}
if (empty ($_POST['password']))
{
    return false;
}
$username = trim ($_POST['username']);
$password = trim ($_POST['password']);
$scrambled = md5 ($password . 'foo');
$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
    $error = "Unable to connect to the database server";
    include 'error.html.php';
    exit ();
}
if (!mysqli_set_charset ($link, 'utf8'))
{
    $error = "Unable to set database connection encoding";
    include 'error.html.php';
    exit ();
}
if (!mysqli_select_db ($link, 'foo'))
{
    $error = "Unable to locate the foo database";
    include 'error.html.php';
    exit ();
}
$sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
$result = mysqli_query ($link, $sql);
if (!$result)
{
    return false;
    exit ();
}
$row = mysqli_fetch_array ($result);
if ($row[0] > 0)
{
    return true;
}
else
{
    return false;
}
}
if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}
?>
some more html code
</html>

When i run it, it says Parse error: syntax error, unexpected end of file in the line where I have written . What's the problem?

share|improve this question
I would imagine you have an error with some of your PHP code... which you haven't posted. – Yacoby Jul 14 '12 at 9:22
It sounds like you have a missing '}', ';' or bracket but it would help if you posted you whole php code – ryanc1256 Jul 14 '12 at 9:23
Please post a complete example – phihag Jul 14 '12 at 9:23
I have added the code – pratnala Jul 14 '12 at 9:25
As @Ryanc1256 mentioned, it is most probably a case of missing bracket. Do include the actual code with the question, if you really want a solution. – Joyce Babu Jul 14 '12 at 9:25
show 1 more comment

closed as too localized by tereško, Jocelyn, cryptic ツ, nickhar, Rachel Gallen May 12 at 0:40

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

3 Answers

up vote 11 down vote accepted

You should avoid this (at the end of your code):

{?>

and this :

<?php}

You shouldn't put brackets directly close to the open/close php tag, but separate with space:

{ ?>
<?php {
share|improve this answer
Thanks!!! This worked!! Thank you – pratnala Jul 14 '12 at 9:34

OK i saw some error's which I have fixed so... from what i got it was this error part

if (login())
{?>
<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>
<?php}
else
{
echo "Incorrect login details. Please login";
}

your new code This is how i would do it any way

<html>
    some code
<?php
function login()
{
    if (empty ($_POST['username']))
    {
        return false;
    }
    if (empty ($_POST['password']))
    {
        return false;
    }
    $username = trim ($_POST['username']);
    $password = trim ($_POST['password']);
    $scrambled = md5 ($password . 'foo');
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
        $error = "Unable to connect to the database server";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_set_charset ($link, 'utf8'))
    {
        $error = "Unable to set database connection encoding";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_select_db ($link, 'foo'))
    {
        $error = "Unable to locate the foo database";
        include 'error.html.php';
        exit ();
    }
    $sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
    $result = mysqli_query ($link, $sql);
    if (!$result)
    {
        return false;
        exit ();
    }
    $row = mysqli_fetch_array ($result);
    if ($row[0] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if (login())
{
echo '<h2>Welcome Administrator</h2>
<a href=\"upload.php\">Upload Files</a>
<br />
<a href=\"points.php\">Edit Points Tally</a>';
}
else
{
    echo "Incorrect login details. Please login";
}
?>
some more html code
</html>
share|improve this answer
Well the thing is inside if (login ()), I have lot of code to write, this is just a sample. Anyway the first answer here has solved it. I didn't know that a space has to be put. Thanks for the help! – pratnala Jul 14 '12 at 9:36
yer just helping anyway yer i did get that but i just like to do it that way. And yer sure – ryanc1256 Jul 14 '12 at 9:38

I have solved your error check it

<?php
function login()
{
    if (empty ($_POST['username']))
    {
        return false;
    }
    if (empty ($_POST['password']))
    {
        return false;
    }
    $username = trim ($_POST['username']);
    $password = trim ($_POST['password']);
    $scrambled = md5 ($password . 'foo');
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
        $error = "Unable to connect to the database server";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_set_charset ($link, 'utf8'))
    {
        $error = "Unable to set database connection encoding";
        include 'error.html.php';
        exit ();
    }
    if (!mysqli_select_db ($link, 'foo'))
    {
        $error = "Unable to locate the foo database";
        include 'error.html.php';
        exit ();
    }
    $sql = "SELECT COUNT(*) FROM admin WHERE username = '$username' AND password = '$scrambled'";
    $result = mysqli_query ($link, $sql);
    if (!$result)
    {
        return false;
        exit ();
    }
    $row = mysqli_fetch_array ($result);
    if ($row[0] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
if (login())
{?>
    <h2>Welcome Administrator</h2>
    <a href=\"upload.php\">Upload Files</a>
    <br />
    <a href=\"points.php\">Edit Points Tally</a>
    <?php
}
else
{
    echo "Incorrect login details. Please login";
}
?>
share|improve this answer
Isn't this the same? – pratnala Jul 14 '12 at 9:34
it is the exact same, I checked – ryanc1256 Jul 14 '12 at 9:35
I does not change any code only solved your errors – Harry Jul 14 '12 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.