0

I have a PHP login page on a website in the following form:

<?php

   $logincorrect = 'notdefined';
   $SubmitLogin = 'no';

   session_set_cookie_params (3600, $httponly = true);
   session_start(); 

   if (isset($_POST['SubmitLogin'])==TRUE)
   {
      $SubmitLogin = 'yes';
      $email = strtolower($_POST['email']);
      $passwrd = md5($_POST['nametag']);
   }
   if($SubmitLogin == 'yes')
   {
      $link = mysql_connect  ('address.website.com', 'restricteduser', 'password_here');
      if (!$link) 
      {
         echo '<p style="color:red">* Error: Could not connect to database.</p> ';
      }

      $email      = mysql_real_escape_string(strip_tags($email),$link);
      $passwrd    = mysql_real_escape_string(strip_tags($passwrd), $link);

      $selectDB = mysql_select_db ('database', $link);
      if (!$selectDB) 
      {
         echo('<p style="color:red">* Error: Could not select database.</p> ' );
         mysql_close($link);
      }

      $passwrdcheck = mysql_query 
      (" SELECT * FROM tablename WHERE email = '$email' AND passwrd = '$passwrd'");
      if (!$passwrdcheck) 
      {
         echo('<p style="color:red">* Error: Could not search database.</p> ' );
         mysql_close($link);
      }

      if(mysql_num_rows($passwrdcheck) == 0)
      {
         $logincorrect = 'no';
         echo '<p style="color:red">Login details incorrect. Please try again.</p>';     
      }
      else
      {
         $logincorrect = 'yes';
         if($profile = mysql_fetch_array($passwrdcheck));
         {
            if($profile = mysql_fetch_array($passwrdcheck));
            {
                $ID = $profile['ID'];

                $languages = mysql_query 
                (" SELECT * FROM translanguages WHERE ID = '$ID' ");
                if (!$languages) 
                {
                    echo('<p style="color:red">* Error: Could not search database.</p> ' );
                    mysql_close($link);
                }

                $expertise = mysql_query 
                (" SELECT * FROM transexpertise WHERE ID = '$ID' ");
                if (!$expertise) 
                {
                    echo('<p style="color:red">* Error: Could not search database.</p> ' );
                    mysql_close($link);
                }

                $tracking = mysql_query 
                (" SELECT * FROM transtracking WHERE ID = '$ID' ");
                if (!$tracking) 
                {
                    echo('<p style="color:red">* Error: Could not search database.</p> ' );
                    mysql_close($link);
                }

                if($profile2 = mysql_fetch_array($languages))
                {   if($profile3 = mysql_fetch_array($expertise))
                    {   if($profile4 = mysql_fetch_array($tracking))
                        {
                            $_SESSION['profile'] = $profile;
                            $_SESSION['profile2'] = $profile2;
                            $_SESSION['profile3'] = $profile3;
                            $_SESSION['profile4'] = $profile4;
                            //echo "Login successful. If you are not automatically redirected, please click <a href='profile.php'> here </a>.";
                            header ("Location: ./profile.php"); exit;
                        }
                    }
                 }
              }
           }
      }
   }
?>

This works fine from my PC and my android phone. However, it does not work from my ipad. I have tried chrome and safari on the ipad and the result is the same: when clicking the 'SubmitLogin' button, the login page just appears to refresh instead of connecting to the database and checking the entered information. No error messages are shown.

I have racked my brain for days, but can't work out why this would be. Any ideas? Thanks!


Edit: The 'directing back to the login page' part of profile.php is as follows:

$loggedin = 'no';
session_set_cookie_params (3600, $httponly = true);
session_start();     

if(isset($_SESSION['profile']) AND isset($_SESSION['profile2']) AND isset($_SESSION['profile3']) AND isset($_SESSION['profile4'])) 
    {
        $profile = $_SESSION['profile'];
        $profile2 = $_SESSION['profile2'];
        $profile3 = $_SESSION['profile3'];
        $profile4 = $_SESSION['profile4'];
        $loggedin = 'yes';
    } 
    else 
    {
        session_destroy();
        header ("Location: ./login.php"); 
        exit;
    }
RLJ
  • 13
  • 1
  • 7
  • Check if cookies are allowed on your iPad. – todinov Nov 26 '15 at 18:26
  • thanks for suggestion. yes they are. also I actually have some php code that checks whether cookies are enabled, and this also indicates that cookies are enabled. – RLJ Nov 26 '15 at 18:31
  • You don't appear to be setting a cookie (which wouldn't work anyhow if you are redirecting immediately). You are just assigning the query result to $ID and then doing nothing with it. Does profile.php check to see if the user is logged in and then send the user back to the login script if they aren't? – Tony DeStefano Nov 26 '15 at 18:34
  • ah you're onto something there! I do actually set some cookies (I left out that part in the interest of making the code clearer to understand - I have now corrected. profile.php does indeed check to see whether the cookies are set and if not directs back to the login page. when I temporarily disable to directing back to the login page part of profile.php then the login code works in the sense that it actually goes to profile.php and stays there, but it doesn't display the correct information. It does appear as though the cookies are not being set correctly. – RLJ Nov 26 '15 at 18:41
  • 1
    We might need the code in profile.php too. Also, a long shot, but try removing the closing php tag (?>). – todinov Nov 26 '15 at 18:44
  • OK I have added the 'directing back to the login page' part of profile.php above. It seems that for some reason the 'else' condition is being triggered - but only on ipad. – RLJ Nov 26 '15 at 18:49
  • removing the closing php tag (?>) just breaks the page – RLJ Nov 26 '15 at 18:51
  • Well, if you have something after the php code, it will. I assume you have session start in profile.php. Try dumping the session var in the else statement instead of redirecting and destroying it. The dump should tell if there is a problem with the session. – todinov Nov 26 '15 at 18:53
  • Yes, I have included the session start in profile.php also. When I disable the else statement, there is code further down in profile.php which prints variables stored in $profile, $profile2, $profile3 and $profile4 to the screen. Nothing is printed, indicating that nothing is stored in $_SESSION['profile'], $_SESSION['profile2'], SESSION['profile3'], nor $_SESSION['profile4'] – RLJ Nov 26 '15 at 18:59
  • I hope this is (equivalent to) what you mean. Apologies - I'm a newbie, so I don't quite understand what 'dumping' means – RLJ Nov 26 '15 at 19:00
  • @TonyDeStefano - you mentioned above that setting a cookie would not work if I am redirecting immediately afterwards, which seems to be what I am doing. Could the error lie herein? – RLJ Nov 26 '15 at 19:03
  • Try running this on your iPad, it should redirect and print "it works": – todinov Nov 26 '15 at 19:08
  • I added a semicolon after the print command and a closing php tag. Ran it from PC and ipad - it worked in both cases. – RLJ Nov 26 '15 at 19:13
  • Yeah, sorry about the semicolon. You don't need a closing PHP tag btw http://stackoverflow.com/questions/3219383/why-do-some-scripts-omit-the-closing-php-tag If this script is working, I suggest you start by rebuilding your code step by step and testing it each time on the iPad. It might take some time, but you will be able to spot what is triggering the error. – todinov Nov 26 '15 at 19:17
  • You can redirect after setting a session value. Just not after setting a cookie value. – Tony DeStefano Nov 26 '15 at 19:29
  • Solved it! I put "sleep(1);" after setting the cookies in the login script, before the redirect. Now it works, also from ipad. – RLJ Nov 26 '15 at 19:31
  • Thanks for your help! I don't know what the custom is - should I put this as an answer? – RLJ Nov 26 '15 at 19:32

0 Answers0