Hey, I just got my site hosted, but it's not working right. Code that worked fine in wamp doesn't work at all. I set up my db in DreamHost. The db is called frontpageads. It's got all the tables needed. When I load my url I get errors that make me think it's a connection error. Line 22 of my code is just a pdo instantiation: $db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); PHP: Here's my connection data: $dbHost = "frontpageads.ca"; $dbPort = "22"; $dbUsername = ""; $dbPassword = ""; $dbName = "frontpageads"; $dsn = "mysql:host=$dbHost;port=$dbPort;dbname=$dbName"; PHP: I've only taken my password and username out. Should this code work? What should I look for, for fixes? Thanks for your time, Jeremy.
Thanks popsicle You mean port 22 sounds weird? I just chose that number because that's what I use to connect with FileZilla to the server.
Uh, what? Are you... Wtf? SQL doesn't run on the same port as the ftp server. Unless you've gotten a specific port for the connection in the documentation from the host, leave it blank
I get less errors, but still no page load: SQLSTATE[HY000] [2002] Connection refused Fatal error: Call to a member function prepare() on null in /home/jeremybenson11/frontpageads.ca/index.php on line 26 php 5.6
if that's the case, you have another problem. Connection refused means that it won't connect with whatever credentials you're using (normally) - are you absolutely sure you've put in everything in the info-doc from Dreamhost into your setup? I'm guessing that the $dbHost might not be complete. Or you have gotten another port you have to use. However, I guess that this is something Dreamhost-support will be able to clear up for you quite fast.
Sadly, DreamHost blamed it on something else... code or something. No support. I switched hosts. The site runs now, but there are issues running some of the scripts. I think I coded in php 5.6 but the server has a step blow and 7.0. I need password_hash(). I'm trying to run a reg script on 7.0 but I get an error, some kind of internal error. They suggest there is a code problem... Something not compatible from 5.6 to 7.0... This is the script I'm running <?php require('../../data/sqldata.php'); require('../classes/pValidator.php'); session_start(); $errors = array(); $validator = new pValidator; $validator->test_blank($_POST['birthMonth'], 'dob month'); $validator->test_blank($_POST['birthDay'], 'dob day'); $validator->test_blank($_POST['birthYear'], 'dob year'); $validator->test_blank($_POST['country'], 'country'); $validator->test_length($_POST['username'],4, 16, 'username'); $validator->test_length($_POST['firstName'],1, 32, 'first name'); $validator->test_length($_POST['lastName'],1, 32, 'last name'); $validator->validate_email($_POST['email']); $validator->test_length($_POST['password'],4,16, 'password'); $validator->test_length($_POST['password2'],4,16, 'password'); $validator->validate_password($_POST['password'], $_POST['password2'], 'password'); $errors = $validator->return_errors(); // test is username or email was used try{ $db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); }catch(\PDOException $e){} $sqlTest = $db->prepare("SELECT count(*) FROM `users` WHERE `username` = ? OR `email` = ?"); $sqlTest->execute(array($_POST['username'], $_POST['email'])); $userCount = $sqlTest->fetchColumn(); if($userCount > 0) { // header back to register with error header('../../register.php?error='.urlencode('username taken, or email used').''); exit; } if(empty($errors)) { // register the user $ip = $_SERVER['REMOTE_ADDR']; $passwordHash = password_hash($_POST['password'], PASSWORD_DEFAULT); $verCode = md5(mcrypt_create_iv(12)); $dob = $_POST['birthDay'] . '-' . $_POST['birthMonth'] . '-' . $_POST['birthYear']; $sqlReg = $db->prepare("INSERT INTO `users`(`username`, `firstName`, `lastName`, `email`, `passwordHash`, `dob`, `verCode`, `verified`, `membership`, `lastLogin`, `lastIp`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); $sqlReg->execute(array($_POST['username'], $_POST['firstName'], $_POST['lastName'], $_POST['email'], $passwordHash, $dob, $verCode, 'no', 'basic', time(), $ip)); $_SESSION['ID'] = $db->lastInsertId(); $_SESSION['serial'] = time(); // send verification code $msg = 'Welcome to Front Page Ads. Please enter the code at the link below with login details. \n Vercode: '.$verCode.''; // use wordwrap() if lines are longer than 70 characters $msg = wordwrap($msg, 70); $headers = "From: webmaster@frontpageads.ca" . "\r\n"; // send email mail($_POST['email'],"frontpageads registration",$msg, $headers); header('Location: ../../verify.php'); exit; }else{ // go back for form with error header('../../register.php?error='.urlencode($errors[0]).''); exit; } ?> PHP:
I think the first place to look is password_hash(). It dropped the PASSWORD_DEFAULT... I'm not sure how to replace it. http://www.serverphorums.com/read.php?7,1197575 http://php.net/manual/en/function.password-hash.php Any ideas? Thanks.
Okay, this is weird. Aside from the server on my domain throwing an internal error without description I'm getting problems in WAMP. I decided to go back and check my code. When I send the regform with the following code I get a blank screen. I have <h2> around line 46 that should output. I'm getting nothing. This code is set up for debugging, so you'll see various header tags along the way, nothing pops up. <?php require('../../data/sqldata.php'); require('../classes/pValidator.php'); session_start(); $errors = array(); $validator = new pValidator; $validator->test_blank($_POST['birthMonth'], 'dob month'); $validator->test_blank($_POST['birthDay'], 'dob day'); $validator->test_blank($_POST['birthYear'], 'dob year'); $validator->test_blank($_POST['country'], 'country'); $validator->test_length($_POST['username'],4, 16, 'username'); $validator->test_length($_POST['firstName'],1, 32, 'first name'); $validator->test_length($_POST['lastName'],1, 32, 'last name'); $validator->validate_email($_POST['email']); $validator->test_length($_POST['password'],4,16, 'password'); $validator->test_length($_POST['password2'],4,16, 'password'); $validator->validate_password($_POST['password'], $_POST['password2'], 'password'); $errors = $validator->return_errors(); // test is username or email was used try{ $db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); }catch(\PDOException $e){ echo "Error creating $db:" . $e->getMessage();} $sqlTest = $db->prepare("SELECT count(*) FROM `users` WHERE `username` = ? OR `email` = ?"); $sqlTest->execute(array($_POST['username'], $_POST['email'])); $userCount = $sqlTest->fetchColumn(); if($userCount > 0) { // header back to register with error header('../../register.php?error='.urlencode('username taken, or email used').''); exit; } var_dump($errors); echo '<h2>Validation done.</h2>'; if(empty($errors)) { // register the user echo '<h2>Send to db.</h2>'; $ip = $_SERVER['REMOTE_ADDR']; $passwordHash = password_hash($_POST['password'], PASSWORD_DEFAULT); $verCode = md5(mcrypt_create_iv(12)); $dob = $_POST['birthDay'] . '-' . $_POST['birthMonth'] . '-' . $_POST['birthYear']; try{ $sqlReg = $db->prepare("INSERT INTO `users`(`username`, `firstName`, `lastName`, `email`, `passwordHash`, `dob`, `verCode`, `verified`, `membership`, `lastLogin`, `lastIp`) VALUES (?,?,?,?,?,?,?,?,?,?,?)"); $sqlReg->execute(array($_POST['username'], $_POST['firstName'], $_POST['lastName'], $_POST['email'], $passwordHash, $dob, $verCode, 'no', 'basic', time(), $ip)); $_SESSION['ID'] = $db->lastInsertId(); }catch(\PDOException $e){echo "error sending data:" . $e->getMessage();} $_SESSION['serial'] = time(); // send verification code $msg = 'Welcome to Front Page Ads. Please enter the code at the link below with login details. \n Vercode: '.$verCode.''; // use wordwrap() if lines are longer than 70 characters $msg = wordwrap($msg, 70); $headers = "From: webmaster@frontpageads.ca" . "\r\n"; // send email mail($_POST['email'],"frontpageads registration",$msg, $headers); header('Location: ../../verify.php'); exit; }else{ // go back for form with error echo '<h2>Errors in $errors</h2>'; header('../../register.php?error='.urlencode($errors[0]).''); exit; } ?> PHP:
Alright, the error in local host is the db connection. I found the error by commenting out code. try{ $db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); }catch(\PDOException $e){ echo "Error creating $db:" . $e->getMessage();} $sqlTest = $db->prepare("SELECT count(*) FROM `users` WHERE `username` = ? OR `email` = ?"); $sqlTest->execute(array($_POST['username'], $_POST['email'])); $userCount = $sqlTest->fetchColumn(); PHP: Connection Vars $dbHost = "localhost"; $dbPort = "3306"; $dbUsername = "root"; $dbPassword = ""; $dbName = "frontpage"; $dsn = "mysql:host=$dbHost;port=$dbPort;dbname=$dbName"; PHP: It doesn't make sense though. My hompage makes a db connection and works fine.
Ugh, this is now solved. I have no idea what issue was. I kept tweaking things here and there. Thanks for the comments.