0

Please shed some light on this question!

I've got a mysql database and it has a few users(maximum 5) to be authenticated. I don't want to make a Mysql-Table user-authentication method for my own security reasons. Just the Top level mysql Users.

In my project there are two .php pages; index.php and connection.php; Within index.php a pop-up username and password Form appears and ask the user to input the username and password. By submitting the Form connection.php is called and it returns back to index.php file again to show more contents.

<?php
$link = mysql_connect("localhost", $_POST['UN'], $_POST['PW']);
mysql_select_db("databaseName", $link); 

if($link){
    header("Location: {$_SERVER['HTTP_REFERER']}");
}
?>

I want to be able to use $link variable in any page that includes the connection.php file and not make a new connection (It is obvious require("/connection.php") alone, in the bellow code below is silly).

<?php
require("/connection.php"); 
if(is_resource($link) != 'mysql link'  || get_resource_type($link) != 'mysql link')
{
  echo '<script>call-the-popup-form-again();</script>'; 
}else{Do-What-It-is-need-to-be-done();}
?>

As you see $link must act as a global variable to hold the connection link. Would you please explain how it is possible to implement this properly?

Thanks in advance

  • Welcome to Stack Overflow! Your code above looks like it should work fine. `$link` will have global scope because it is defined outside a function. Your question is not related to authentication or Zend Framework and you should remove those tags. Site users tend to get upset if you spam tags which are unrelated to the question. Also the `mysql_*` functions in PHP are deprecated and shouldn't be used. [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) has information on why and what to replace them with. – Matt Raines May 19 '16 at 20:28
  • Matt, Thanks for welcoming me. Unfortunately the code does not work and I think it is because $link does not keep its value which is ResourceID at all. – LongTermInter May 19 '16 at 21:00
  • I've read this through properly and the structure needs a ground-up rethink. You'll need to submit the form somewhere else to validate the username and password and then you'll need to save it in a [session](http://php.net/manual/en/book.session.php) so that you can access it in future requests. Your connection.php include should use the session variables to connect. Your logic of redirecting to the previous page when the connection is successful won't work because when you return to the index page your POST variables will no longer be set so the connection will fail. – Matt Raines May 19 '16 at 22:04
  • Dear Matt, Really sorry for my bad PHP(I am a c++ programmer) so I am not sure about what I am saying: If I added session variable to my connection.php wouldn't this bring $link from its global scope and not accessible trough other pages? – LongTermInter May 19 '16 at 22:13
  • It's important to realise that each web request does not retain state from previous requests, so when you redirect using the `header` function, you have to reopen your MySQL connection in the new request. – Matt Raines May 19 '16 at 22:15
  • I would be really grateful if you help me with the logic and maybe some code. Definitely, when I got it working I will post a clean code for others to use and learn. – LongTermInter May 19 '16 at 22:22
  • Can anyone help me with this? – LongTermInter May 20 '16 at 06:13

0 Answers0