PHP Sessions Help - Noobie :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Karpaty
Forum Newbie
Posts: 1
Joined: Tue Dec 15, 2009 6:58 am

PHP Sessions Help - Noobie :(

Post by Karpaty »

Hi all,

Got a problem with my php Login page - whenever I click on "Login" a blank page appears, whereas it should take me back to the home page and replace "Login" button with the "Logout" button.

The code is as follows:

Login.php

Code: Select all

 
<?php
session_start();
include("db_open.php");
//error_reporting(0); 
 
if ($_GET["op"] == "login")
 {
 if (!$_POST["username"] || !$_POST["password"])
  {
  die("You need to provide a username and password.");
  }
  // Create query
 $q = "SELECT * FROM `members` "
  ."WHERE `username`='".$_POST["username"]."' "
  ."AND `password`='".$_POST["password"]."' "
  ."LIMIT 1";
 // Run query
 $r = mysql_query($q);
 
 if ($obj=@mysql_fetch_object($r))
  { // Login good, create session variables
  $_SESSION["valid_id"]=$obj->id;
  $_SESSION["valid_user"]=$obj->username;
  $_SESSION["valid_email"]=$obj->email;
  $_SESSION["valid_time"]=time();
  Header("Location:index.php");
  }
 else { // Login not successful
  die("Sorry, could not log you in. Wrong login information.");
  }
 }
else { //If all went right the Web form appears and users can log in
 echo "<form action=\"?op=login\" method=\"POST\">";
 echo "Username: <input name=\"username\" size=\"15\" value=\"Roman\"><br />";
 echo "Password: <input type=\"password\" name=\"password\" size=\"8\" value=\"admin\"><br />";
 echo "<input type=\"submit\" value=\"Login\">";
 echo "</form>";
}
?>
 

Index.php - menu part only

Code: Select all

<?php
error_reporting(0);
session_start();
 
if (!$_SESSION["valid_user"]){
    // if user not logged in, display login link
    echo "<li><a href=\"login.php\">Login</a></li>";
}
else {
    // if user is logged in, display user information and logout link
    echo "<li><a href=\"logout.php\">Logout</a></li>";
    echo "<p>User ID: " . $_SESSION["valid_id"];
    echo "<p>Username: " . $_SESSION["valid_user"];
    echo "<p>Email: " . $_SESSION["valid_email"];
    echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
    $id = $_SESSION["valid_id"];
}
?>
As said above, when I click on login a blank page appears...
I tested the code at home (am using WAMP SERVER),it works perfectly fine!
Then i tried it in the college, it just wouldnt work as intended (blank page)

I believe the session is registering, because when i added the following to the login page, it displays the correct info (see here):

Code: Select all

 echo "Hello " . $_SESSION["valid_user"] . "<br/>";
  echo "UserID " . $_SESSION["valid_id"] . "<br/>";
  echo "Email " . $_SESSION["valid_email"] . "<br/>";
  echo "Time " . $_SESSION["valid_time"] . "<br/>";
Please advice :(
kalthar
Forum Newbie
Posts: 7
Joined: Fri Dec 11, 2009 2:28 pm

Re: PHP Sessions Help - Noobie :(

Post by kalthar »

Check your PHP version, you may need to use session_register() rather than $_SESSION[''] although check your development environment, it's been deprecated as of 5.3.0 and being removed in 6.0.0, if that doesn't work, I haven't a clue.
Post Reply