1

I am having issues with my PHP logout script I have the below code:

<?php
session_start();

//redirect function
function returnheader($location){
    $returnheader = header("location: $location");
    return $returnheader;
} 

$connection = mysql_connect("localhost","username","password") OR die(mysql_error());
$db_select = mysql_select_db("database",$connection) OR die(mysql_error());

// destroy cookies and sessions
setcookie("userloggedin", "");
$username = "";
session_destroy();

//redirect
returnheader("index.php");

?>

I keep getting the below error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user                                  'loginuser'@'localhost' (using password: YES) in logout.php on line 10

Access denied for user 'loginuser'@'localhost' (using password: YES)

can anyone help I am new to php and may of bitten off more than i can chew :)

Kris Edwards
  • 35
  • 2
  • 10
  • Have you verified that the parameters you are using to connect work elsewhere? If so, how? NB MySQL treats 'localhost' as a special case - it doesn't connect via the network to 127.0.0.1, it uses a filesystem socket. – symcbean May 16 '13 at 09:35
  • Login and logout in php using session : http://allitstuff.com/login-and-logout-in-php-using-session/ – jaydeep namera Sep 12 '13 at 06:35

3 Answers3

3

Looks like you have a copy paste from some online tutorial you have to use your username, password and database names in this functions

mysql_connect("localhost","username","password");
mysql_select_db("database",$connection) 
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Hi I have put in the localhost uesrname password myself as I didn't want to put the details on here. I have the correct details in on the actual script – Kris Edwards May 16 '13 at 09:30
  • We are not here to assume that you are not putting exact code. Then you should explain that in question...Still this is something related with your db credentials either you are using wrong password or their is no password required for connection. – chandresh_cool May 16 '13 at 09:31
1

It would be better to include a file which contains database connection stuff instead of writing them on every page. Apart from that, a logout script like this should be sufficient enough:

<?php
    session_start(); // start a session first, else you cannot destroy/unset it
    session_destroy(); // destroy all sessions
    header('location:index.php'); // redirect
?>

Also, as @chandresh_cool said, I hope you didn't really use "username", "password" and "database" as credentials.

Edwin Lambregts
  • 408
  • 6
  • 22
  • +1 for noticing that the database connection isn't even used. – Joachim Isaksson May 16 '13 at 09:33
  • Hi thanks no i didn't use the "username", "password" and "database" as credentials i just put those in :) so if i use a simple connection script and then include that on top of each page and then use the above script to logout that would work? – Kris Edwards May 16 '13 at 09:38
  • That's right. It's also more easily editable because if you switch databases, change your password or whatever, you would only have to edit 1 file (which would be the connection file) instead of looking for those 2 lines in every single file that requires a database connection. :) – Edwin Lambregts May 16 '13 at 09:41
0

If you post your entire code and just change the username and password we can help you better.

It says error on line 10 - but we can't see easily what line 10 is, given you have sliced your code.