file_get_contents authentication gives '401 Access Denied Error' - php

I am trying to create a database in cpanel using the folowing comment
$result =file_get_contents("http://$cpanel_user:$cpanel_password#$cpanel_host:2082/frontend/$cpanel_skin/sql/adddb.html?db=$db_name")
echo $result;
this command gives me HTTP request failed! HTTP/1.1 401 Access Denied error. I have used the correct password and username for connecting. And i am able to create the database when i give this url(url in this format) in a browsers url field.
Can someone explain what seems to be the problem?

That is not the correct way to create databases using cPanel. I believe you are trying to replicate the form inside cPanel to create databases, which is not correct.
Also, you will not be able to create databases using your cPanel username and password. I think you should refer to this thread to resolve your issue.

Related

PHP - Connect to different servers database

I'm developing on 'server1'.
I need to save the data processed on this server to the 'server1' database.
However, I also need to save some data to an external 'server2' database.
The problem is that when I connect to the database of the other server I get a fairly weird error.
Error code: 1045
Error message: Access denied for user 'server2-username'#'server1-name' (using password: YES)
Why does 'server1' appear in the error message?
To me, the message says this: "hey, I can't find the 'server2-username' on 'server1' database". Am I correct? Why does say that, when I'm connecting to 'server2'?
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database');
if($connection->connect_errno) {
echo $connection->connect_errno;
echo $connection->connect_error;
}
Any idea how should I connect to 'server2'? Or what should I check?
This is the first time I want to connect to another server database. I haven't done this yet, and I don't know what's wrong.
This error mean 'server2-username' try to connect from 'server1-name' and not succeed.
Please check permissions user2 on server2 for connect from remote host
It demands an account which matches the connecting user#hostname, which is server2-username#server1-name... it's just that the web-server runs on the same host server1-name as the mySQLd on server1-name. This may appear confusing, but the hostname comes from where the script runs.
You'd need to add user server2-username#server1-name to the mySQLd on server2-name... and if you can't get that account set up, most commonly there's a JSON API available as a web-service; exporting/importing data to any format would also be an option, if they wish to import themselves.
SSL tunneling could even connect through local loop-back interface 127.0.0.1 on :3307 (the problem isn't bind-address = 127.0.0.1, but that there is no such user#hostname available):
shell_exec("ssh -fNg -L 3307:server2-ip:3306 server2-username#server2-ip");
$connection = new mysqli('server2-ip', 'server2-username', 'server2-password', 'server2-database', 3307);
However, either getting that remote user account setup or using an API might rather be the suggested options, because the SSL tunnel created with shell_exec() might be closed at any time, which all needs to be tested & handled accordingly. The error message would at least be server2-username#server2-name or server2-username#localhost, when tunneling into it.

Can't authenticate a cURL request despite supplying username and password

I have a dev URL that is behind HTTP authentication using a simple .htpasswd file - nothing special about that at all, all standard.
I am trying to hit a URL on that domain via cURL but am getting this message of course:
Unauthorized
This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
Apache/2.4.29 (Ubuntu) Server
I have added what I believe to be the correct parameter to authorise the url but with no joy. Just the same message. I have echo'd out the username and password and these are exactly as I would enter into the browser prompt.
What is the next step to debug this?
I have solved this by NOT using curl_setopt_array, instead setting each 3 separately. Cannot currently explain why this is different but it works! :(

MySQL - access denied until I connect via Workbench or CLI

I'm having issues with intermittent connection issues from my localhost PHP site to my localhost MySQL DB. It gives me back Access Denied for 'user'#'localhost' (using password: YES) as is typical for a wrong password. I tested to make sure it wasn't anything particular, and I could get it with root, so I was confused. I even set up a 127.0.0.1, a localhost and a % account with the same username and password. None of them worked, but root worked just fine. That's been my biggest confusion - 'root' working with no issues, but this new user not working at all.
I've looked through so many of the problems (see below) and issues laid out on SE and other forums relating to Error 1045, Access Denied and all variants, and most boil down to a misspelling, or a typo, or something of that nature. I've copy and pasted every relevant string around all of my code, so that's not the issue. I don't have an anonymous user, so that's not the issue. There's no DNS issues, it's localhost and I've tried every iteration of that.
The issue comes from what solves the issue for me. If I open a connection to the DB using the new account via MySQL Workbench or CLI, suddenly the webpage will work with no issues, connecting via the new account with no problems. But if I run the script that changes the new account information whatsoever, even if I change nothing, and reload the page, it will deny the access again until I open that connection up again via MySQL Workbench or the CLI.
It works without fail, every single time. I have no idea what's going on - does the account need to be logged into via CLI or Workbench before they can be used via PHP? Or is there something more I am missing here?
PHP Code
$servername='localhost';
$username='website';
$password='password';
$database='db_1';
$conn=new mysqli($servername, $username, $password, $database);
if($conn->connect_error) die("connection failed");
MySQL Credentials Creation
create user website#'localhost' identified by 'password';
grant SELECT, INSERT, DELETE on db_1.* to 'website'#'localhost';
A few of the SO questions that didn't help:
MySQL - Access denied for user
access denied for user # 'localhost' to database ''
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)" from php function
EDIT 1:
I am running a WAMP stack on a local computer. I have my firewall open for port 3306 (the port MySQL is listening on). I am not using CPanel (I think that's what it's called anyways). And I just tested and it doesn't work with root either until I go ahead and recreate a new connection to the DB via the CLI or Workbench.
The error it's giving back also includes this, but I've never had this come up as I've never tried to install OpenSSL on either before: mysqli::__construct(): PHP was built without openssl extension, can't send password encrypted in [file]. But this goes away as soon as I make the connection via the CLI (no fancy tags or anything, so I don't think I'm using SSL) or via Workbench.
Edit 2: The rabbit hole went deeper and then stopped but I'm still confused as to how to configure my set up properly to avoid it in the future/fix it to be more secure. The solution (which I can't put up as my own, so if one of you wants to explain and then claim this, go for it - I'll accept it) was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method. If someone could explain how to make my PHP compatible with the newer authentication method (I haven't looked yet but will be after I post this) I would be grateful.
The solution was to go back to my MySQL server and reconfigure the Authentication Method to not use the Strong Password Encryption and instead opt for the Legacy Authentication Method.

Can't connect to database using my recently added user in MySQL

I'm trying to add a new user to a mysql database.
The original state of the file looks like this (which works):
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1","XXXXXX");
mysql_select_db("abong1",$dconex);
I'm trying to add this line in between the mysql_select_db:
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1_abongler","XXXXXXXX");
I configured the user in PhpMyAdmin with the correct IP, username, and password, but it doesn't seem to work. Is it something about the username? should it have a 'root' like "abong1.abong1_abongler" ?
Thanks in advance.
You may need to run
flush privileges;
What error are you getting, specifically? Permission denied? Can't connect? Change your connection line to the following:
$dconex=mysql_connect("200.xxx.xxx.xxx","abong1_abongler","XXXXXXXX") or die(mysql_error());
and see what error gets spit out. If it's permission denied, then your user isn't configured properly (could be username, hostname, or password is incorrect). Connection refused would be MySQL not allow connections over TCP.

Reasons for MySQL authentication error: "Access denied for user 'xxx'#'yyy'"?

What possible reasons could exist for MySQL giving the error “Access denied for user 'xxx'#'yyy'” when trying to access a database using PHP-mysqli and working fine when using the command-line mysql tool with exactly the same username, password, socket, database and host?
Update:
There were indeed three users in the mysql.user table, each one with a different host (but with the same hashed password), one was set to localhost, one to 127.0.0.1 and one to the machine’s host name. Deleting two of them and changing the host of the third to “%” had only one effect: now the access is denied using the command-line tool also.
I did do a
select user();
before that in the command line and it yielded the same xxx#yyy that were denied in php.
Sometimes in php/mysql there is a difference between localhost and 127.0.0.1
In mysql you grant access based on the host name, for localusers this would be localhost.
I have seen php trying to connect with 'myservername' instead of localhost allthough in the config 'localhost' was defined.
Try to grant access in mysql for 127.0.0.1 and connect in php over 127.0.0.1 port 3306.
In case anyone’s still interested: I never did solve this particular problem. It really seems like the problem was with the hardware I was running MySQL on. I’ve never seen anything remotely like it since.
After I read your update I would suspect an error in/with the password.
Are you using "strange" characters in your PW (something likely to cause utf-8/iso encoding problems)?
Using % in the Host field would allow the user to connect from any host. So the only thing that could be wrong would be the password.
Can you create another user. whith the "grant all on all for '...'#'%' identiefied by 'somesimplepw'" syntax, and try to connect with that user?
Don't forget to 'flush privelidges'
For info on how to create a new user klick here
Today the FTP service of my web hosting provider is having some trouble, so I decide to realize a local virtual webserver for working on my website. I have installed EasyPHP 14.1 with phpMyAdmin and I have created my user and my database with tables.
First attempt: failed. I realized that the table I was looking for didn't exist. -> I solved creating the missing table.
Second attempt: failed. I realized that username that I set for my new user was diffrent from the username I used for my connection. -> I edited username.
Third attempt: failed. I realized that new database name was diffrent from database name I use for connection in my site. -> I edited db name.
Forth attempt: failed. I realized that between privileges of my new user there wasn't "Grant". I don't even know what "Grant" means, but let's try to enable it -> Added "Grant" privilege.
Fifth attempt: I win!
I hope my little adventure could help someone. =)

Categories