user2475515
user2475515

Reputation: 17

What is wrong with this code? How can i correct it?

I'm completely new to PHP. So yeah this code might totally make no sense. But anways. I want to ask for help. If someone can tell me what is wrong and or just write correct code.

Basicly what i try make code to do is check if user is logged in, if not then show he login and register link.

If he is logged in then show him his username.

Here is PHP code:

<?php

    require("common.php");

    if(empty($_SESSION['user']))
    {
        echo("<a href="login.html"><b>Login</b></a>");
        echo("<a>or<a>");
        echo("<a href="register.html"><b>Register</b></a>");
    }

    else
    {
        echo 'Username: ' . $current_user->user_login . "\n";
    }
?>

Code from HTML:(Code that i use to include this code. Not sure if its also...)

<header action="php/check.php" method="post">

Thanks for answers Still not working tho :( Here is current code, I edited how you guys adviced.

<?php
    session_start()

    require("common.php");

    if(empty($_SESSION['user']))
    {
        echo '<a href="login.html"><b>Login</b></a>';
        echo '<a>or<a>';
        echo '<a href="register.html"><b>Register</b></a>';
    }

    else
    {
        echo 'Username: ' . $current_user->username . "\n";
    }
?>

Upvotes: 0

Views: 105

Answers (3)

Maresh
Maresh

Reputation: 4712

Are you sure session are starting automatically? Otherwise you need to add this:

Edit: And also remove the parentethis around echo, it's built-in the language, it's not a function. And escape quotes as well.

<?php
    session_start()

    require("common.php");

    if(empty($_SESSION['user']))
    {
        echo "<a href=\"login.html\"><b>Login</b></a>";
        echo "<a>or<a>";
        echo "<a href=\"register.html\"><b>Register</b></a>";
    }

    else
    {
        echo 'Username: ' . $current_user->user_login . "\n";
    }
?>

You can also add a php.ini and use session auto_start.

http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start

Upvotes: 2

Kevin
Kevin

Reputation: 6711

First, you need to escape the double quotations in all the escape statements as this:

echo "<a href=\"login.html\"><b>Login</b></a>";
echo "<a>or<a>";
echo "<a href=\"register.html\"><b>Register</b></a>";

You don't need the parentheses for echo.

Second, in order to use the $_SESSION variable, you need to start the session before that using the function session_start().

Upvotes: 0

Jairo Filho
Jairo Filho

Reputation: 342

Try this:

if(empty($_SESSION['user']))
{
    echo '<a href="login.html"><b>Login</b></a>';
    echo ' or ';
    echo '<a href="register.html"><b>Register</b></a>';
}

Upvotes: 0

Related Questions