SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can anyone spot my login script problem??

    Hi,

    Im having this really weird problem....it doesnt happen all the time and i can usually get success using opera.....weird..

    I have a login script:

    Code PHP:
    // Retrieve the user_id and first_name for that username/password combination.
    		$query = "SELECT user_id, first_name FROM users_home WHERE username='$u' AND password=PASSWORD('$p')";		
    		$result = @mysql_query ($query); 
    	  echo mysql_error(); 
    		$row = mysql_fetch_array ($result); 
     
     
    		if ($row) { 
     
     
     
    				// Start the session
     
    				session_start();
    				 $_SESSION['user_id'] = $row['user_id'];
    				 $_SESSION['first_name'] = $row['first_name'];
    				 $_SESSION['timestamp'] = time();
     
    				 //echo $_SESSION['first_name'];
    				 //exit();
     
    				header ("Location:  http://www.website.com/home.php");

    NOw if i enter the wrong details the database is checked and i get a message sent back saying 'sorry your details were incorrect...."

    If i put some correct login details in the redirect to home where this bit of code is run:



    Code PHP:
    session_start();
    print_r($_SESSION);
    exit();
      if (!isset($_SESSION['first_name'])) {
        header ("Location:  http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "index.php?failed=1");
    	  exit(); // Quit the script.
      }

    Now im currently print_r the session array and then exiting and indeed if i try to login using FF or IE i just get a blank array....if i try using opera i can usually get in....about a week ago i could login using opera....at work i can mostly login using FF but sometimes it takes two or three attempts......its a really weird problem but for some reason the session variables dont get set at index.php sometimes...

    Any ideas? I can post more code if need be

    thanks for reading

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take the session _start() out of this code snippet and put it @ the start of each page that is accessed.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe it isn't your php but your html. Just a thought but after you post your form print_r the $_POST array to make sure there are values in it. Maybe you have a bit of bad html and some browsers aren't able to post the data.

    Also, you might want to turn on all errors/warnings to see if you can spot what might be causing the problem in php.
    error_reporting(E_ALL);

    Finally, when checking to see if a valid record was returned fromt the database you might want to do the following:
    $result = @mysql_query ($query);
    if (mysql_num_rows($result) == 1){
    $row = mysql_fetch_array ($result);

    That way you don't call the mysql_fetch_array() function on an empty data set.
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •