0

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";
    }
?>
Sam
  • 7,252
  • 16
  • 46
  • 65

3 Answers3

2

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

Maresh
  • 4,644
  • 25
  • 30
0

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>';
}
Jairo Filho
  • 342
  • 5
  • 17
0

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().

Kevin
  • 6,539
  • 5
  • 44
  • 54
  • I did that. Still not working. I was wondering do i need some html on php file. or php to html? so currently my HTML file have:
    So im trying to print that php file answers to header... At this moment it won't print it anywhere.
    – user2475515 Jun 11 '13 at 17:41
  • did u give the file path correctly? `action="php/check.php"` is relative to the current location. Use a `/` before it. It goes like `action="/php/check.php"`. If it doesn't work, take a look at this to get the root's location: http://stackoverflow.com/questions/17036192/when-new-page-is-created-the-css-is-not-taken-into-account/17036267#17036267 – Kevin Jun 11 '13 at 17:45
  • 1
    @user2475515 I've never seen `
    ` before. `
    ` yes, but *header*?
    – Funk Forty Niner Jun 11 '13 at 17:45
  • @Fred How did I not notice it? -_- – Kevin Jun 11 '13 at 17:47
  • I made it to
    Still not working tho :(
    – user2475515 Jun 11 '13 at 17:51
  • @user2475515 The easiest way to find this out is to turn error_reporting on. Open your `php.ini` file and search for `error_reporting`. Set to `E_ALL`. If it is already set, remove the semicolon `;` at the beginning of the line (to remove the comment). – Kevin Jun 11 '13 at 17:56
  • @KevinPaladin Don't know Kevin. I thought there was something I didn't know about. PHP seems to get trickier by the hour lol! – Funk Forty Niner Jun 11 '13 at 17:57
  • @user2475515 Try to execute your form from within the same folder and test it out from there. Might just be a `path` issue. On an added note, you're going to need `session_start();` in all affected/touched pages for sessions to work properly. – Funk Forty Niner Jun 11 '13 at 17:58
  • I made ;error_reporting to -> E_ALL error_reporting Where does it write errors?(Don't see any at site) I also tried to move check.php to same folder than html file is. Also edited it to run from same direction. Doesnt seem to work even still... – user2475515 Jun 11 '13 at 18:01
  • try adding this line instead of the line you edited. `error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT` When you process the php file, the errors should be displayed on theat page (if any) – Kevin Jun 11 '13 at 18:09
  • @user2475515 See my latest comment at the top of your question about `session_start`. – Funk Forty Niner Jun 11 '13 at 18:14
  • sorry, took while to answer... added session_start(); like u told me to. still does not work. Added error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT to my PHP config file. It didn't write any errors to my check.php. After refreshing site. (If u meant that with process the php file) – user2475515 Jun 11 '13 at 18:47
  • ahh.. u need to restart apache after changing `php.ini`. Sorry, forgot to mention that. – Kevin Jun 11 '13 at 18:50
  • The errors should be displayed on the browser, if encountered. Why don't post all the files' script in pastebin and give their references. Looks like it is the only way to find the problem. – Kevin Jun 11 '13 at 19:00
  • Okay. I uploaded files to my SkyDrive. Here is link to you. https://skydrive.live.com/redir?resid=6854030AFCED54A7!114&authkey=!AJs92enZxMkkbOY (Hope it works) – user2475515 Jun 11 '13 at 19:08
  • Well, when I ran the file `check.php` it displayed the following error info : `Failed to connect to the database: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)`. I think there are many other errors too and so, I would suggest you to turn on error reporting first without which it is nearly impossible to find the cause, as it seems like the file `common.php` was not written by you. http://stackoverflow.com/questions/6575482/php-how-do-i-enable-error-reporting – Kevin Jun 11 '13 at 19:27