I followed a tutorial on http://www.phpeasystep.com/workshopview.php?id=6 and I think it's not working properly for me. When I try to start the session in my sidebar thing it won't actually start somehow :S

<div id="sidebar">
<h2>[<a href="?page=home">Samppa's page</a>]</h2>
<h3>&nbsp;Main menu</h3>
<ul class="menu">
<li><a href="?page=about"> >> About</a></li>
<li><a href="?page=projects"> >> Projects</a></li>
<li><a href="?page=photos"> >> Photos</a></li>
<li><a href="?page=lab"> >> THE testing lab</a></li>
</ul>

<h3>&nbsp;Anything else</h3>
<ul class="menu">
<li>Email me!<a href="mailto:samppa.l@hotmail.com"><br /><img src="mail.png" width="210" height="210" /></a></li>
<li><br />Have a question?<a href=""><br /><br /><img src="question.png" width="210" height="210" /></a></li>
</ul>

<h3>&nbsp;Login</h3>
<ul class="menu">
<br />
<?
session_start();
echo $_SESSION['myusername'];
?>
<form name="form1" method="post" action="checklogin.php">
<li>Username: <input name="myusername" type="text" id="myusername" /></li>
<br />
<li>Password: <input name="mypassword" type="text" id="mypassword" />
<br />
<br />
<li><input type="submit" name="Submit" value="Login" /></li>
</ul>

</div>

It won't echo the username I don't understand why, and if I try with <?php and ?> instead of <? ?> I get an error, I am really noob in this please help, and also look the link I gave then you get idea.

Dani AI

Generated

Likely causes here are (a) short PHP tags being ignored, (b) session_start() being called after output (including stray whitespace or a UTF-8 BOM), (c) the login script never setting the session, and (d) the form not being closed. used short tags (<?) which often fail on many servers — use full <?php tags for portability. and were right to flag session placement; correctly noted the missing </form>.

Quick checklist to diagnose and fix

  • Replace any <? with <?php and confirm pages are saved without a BOM or leading spaces.
  • Put session_start() in a single config/include file that is required at the very top of every PHP page (before any HTML or whitespace).
  • In the login handler (action page) call session_start(), validate credentials, then set $_SESSION['myusername'] and redirect with header() + exit so the browser loads the page that reads the session.
  • Close the <form> and use type="password" for password fields.
  • Watch for PHP warnings like “headers already sent” — they tell exactly which file/line emitted output first.

Example skeleton for the login handler (replace the auth check with a real DB lookup and password_verify):

<?php
session_start();
$username = trim($_POST['myusername'] ?? '');
$password = $_POST['mypassword'] ?? '';
// replace with DB check + password_verify
if ($username === 'demo' && $password === 'secret') {
  session_regenerate_id(true);
  $_SESSION['myusername'] = $username;
  header('Location: /'); exit;
}
header('Location: /?login=fail'); exit;
?>

Example sidebar display (must run before output or be in an include loaded at the top):

<?php
session_start();
if (!empty($_SESSION['myusername'])) {
  echo 'Logged in as ' . htmlspecialchars($_SESSION['myusername'], ENT_QUOTES, 'UTF-8');
}
?>

Security notes: never store plain-text passwords, use password_hash() / password_verify(), use prepared statements, and call session_regenerate_id(true) on successful login.

Recommended Answers

All 5 Replies

I see 2 things:

1. in the 2nd line try to remove the '
2. You never close the form </form>

Still won't work even simple echo doesn't work in that <? ?> but if I do <?php it works.. but if I put session start there I get error

better use echo statement in check login.php
Welcome <?php echo $_POST; ?>!<br />i hope it will work

One thing I can see is that your session_start() should be the first thing on the page, as nothing can be echoed or written to the browser before the session actually starts.

plus, close your form tag

As BrianDickson said session_start() should be the first line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.