php code show blank screen in godaddy web hosting - php

<?php
ini_set('error_reporting', E_ALL);
//create empty Variable
$username= $password= $email="";
//check the field is not empty
if (empty(($_POST["Username"]))) {
echo "Username should not be empty";
} else {
if (!empty(($_POST["Username"]=="/^[a-zA-Z0-9_-.]{3,15}$/"))) {
echo "Username should contain character,number,dash,underscore,dot only";
}
}
if (empty(($_POST["Password"]))) {
echo "password should not be empty";
} else {
if (!empty(($_POST["Password"]=="/^[a-zA-z0-9#]{3,15}$/"))) {
echo "Password should contain character,number,# only";
}
}
if (empty($_POST["Email"])) {
echo "Email field should not be empty";
} else {
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email = "Invalid email format";
}
}
//connect to Mysql Database
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->select_db("spinjobs");
//check if the username already exits in database table
if(isset($_POST["$username"]) && $_POST["$username"] != ""){
$sql_username = mysqli_query("SELECT id FROM newuser WHERE username='$username' LIMIT 1");
$username = mysqli_num_rows($sql_username);
if ($username < 1) {
echo '<strong>' . $username . '</strong> is OK';
exit();
} else {
echo '<strong>' . $username . '</strong> is taken';
exit();
}
}
$query = "INSERT INTO `newuser` ( `username`, `email`, `password`) VALUES ( '$username', '$email', '$password')";
$result = mysqli_query($conn, $query);
if (mysqli_affected_rows($conn) == 1) { //If the Insert Query was successfull.
// Send an email
$from = 'donotreply#spinfonet.com';
$to = $email;
$subject ='You are successfully Registered with us';
$message = 'Your $username, $password\r\n Please preserve this mail for your reference';
mail($from, $to, $subject, $message);
}
mysqli_close($conn);
?>
I am using GoDaddy as web host and upload this code file to my webhost space. I have Linux operating system with web host, while I type code on Notepad++ in Windows 10.
When I test my page in browser after filling record in HTML page and submit browser shows blank page, I have checked my PHP code with some of free online code testing website. This website do not find any mistake in code, can you guide what should be the mistake?
GoDaddy gives three version of PHP 5.4, 5.5, and 5.6. I have selected 5.6, 5.4 and 5.5 also have given blank page.

It's very likely that display_errors it's turned off. You can change that in the php.ini file:
display_errors = On
Here's where to find the file:
https://www.godaddy.com/help/what-filename-does-my-php-initialization-file-need-to-use-8913
Most of the time, it's in the root directory.

Solution given on godaddy community:
Steps:
Go to CPanel dashboard
Go to 'Select PHP Version', you should see a bunch of checkboxes
Set PHP version (min 5.4)
Uncheck the 'mysqli' extension and check both 'mysqlind' and 'nd_mysqli'
Apparently 'nd_mysqli' is 'mysqli' but configured to work with 'mysqlind' and to make it work you have to uncheck 'mysqli' due to settings conflicts.
This information was hidden deep within a stackoverflow answer that I can no longer find. Hope this helps some other people.

Make sure these values are correct according to your settings. And use mysqli_connect_error() function:
$servername = "server addresss";
$username = "";
$password = "";
$dbname = "";

Related

Website session error ignoring session_start() when only one session_start is in the code

I have been using this login script on multiple websites on different servers,
this is the first time I get this type of error. I have looked through many web pages trying to solve the issue but anything I try so far didn't solved the problem. I tried to destroy and use new session_id or to regenerate the session_id had no effect, as far as I can tell this error
makes it impossible for me to manipulate sessions.
Also this code seemed to be working before but suddenly stopped working, and have been trying to fix it ever since. I tested this code on other web servers but they seem to be running fine only on the server where my website is hosted doesn't seem to be working.
Notice: A session had already been started - ignoring session_start() in
/home/{website}/public_html/{website}/login.php on line 41.
Any help from veterans would be appreciated.
Here is my code
<?php
ob_start();
// Error Reporting script
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//database script
$mysqli = #mysqli_connect('host', 'user', 'pass', 'database');
if (!$mysqli) {
die('Connect Error: ' . mysqli_connect_error());
}
if(isset($_POST['login-submit'])){
$errors = array();
//Basic validation
if(empty($_POST['customer_email'])){
$errors[] = "Please enter your email";
}else{
$email = $mysqli->real_escape_string($_POST['customer_email']);
$email = htmlspecialchars($email);
$email = strip_tags($email);
}
if(empty($_POST['customer_pass'])){
$errors[] = "Please enter your password";
}else{
$password = trim($_POST['customer_pass']);
$password = htmlspecialchars($password);
$password = strip_tags($password);
}
//Setting up session
if (empty($errors)) {
$sql = "SELECT * FROM joinunion WHERE email = '$email'";
$result = $mysqli->query($sql);
if ($result->num_rows === 1) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (password_verify($password, $row['password'])) {
// start up session
session_id("User-Login");
if(!isset($_SESSION['email']['id']))
{
session_start(); //<======THIS IS WHERE THE
//ERROR IS ' LINE 41'
}
$_SESSION['email']['id'] = $row['id'];
session_write_close();
// direct user to a new page - for testing purposes
//header("Location:index.html");
}else{
$errors[] = "The username or password do not match";
}
}else{
$errors[] = "The username or password do not match";
}
}
}?>
Any help from veterans would be appreciated.

How to display: "Error: no connection" in php when there's no internet connection?

So I'm working on a login/register system with php which I can use for my Android app (working with Android Studio 2.3.1).
I can already register via my phone. But I found out that if I turn off the wifi and my data and I try to register an empty message gets displayed. I would like to fill this empty message with the text: "Error: no connection" but I really don't know how.
My code is:
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$hostname = "hostname";
$username = "username";
$password = "password";
$dbname = "databasename";
$conn = mysql_connect($hostname, $username, $password);
if (!$conn)
{ die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $conn);
$name = $_POST["name"];
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$repeatpassword = $_POST["repeatpassword"];
if($name == '' || $username == '' || $password == '' || $email == ''){
echo 'Please fill in the missing fields';
}else{
$checkusername = mysql_query("SELECT * FROM users WHERE
username='$username'");
$checkemail = mysql_query("SELECT * FROM users WHERE email='$email'");
$testusername = mysql_fetch_array($checkusername);
$testemail = mysql_fetch_array($checkemail);
if($testemail) {
echo 'Email is already used';
}else {
if ($testusername){
echo 'Username already exists';
} else{
if ($_POST["password"] <> $_POST["repeatpassword"]) {
echo 'Passwords do not match';
}else {
if (mysql_query("INSERT INTO users (name, username, password, email) VALUES
('$name','$username', '$password','$email')")){
echo 'Successfully registered. Now try logging in.';
}else{
echo 'Oops! Please try again!';
}
}
}
}
}
mysqli_close($conn);
}
I'm not very experienced with php and its functions yet, or with stackoverflow for that matter and thus I haven't been able to figure out the solution myself yet and I can't find it online because all I'm getting is people having trouble connecting to the wifi, but I don't have that. I just want a message to get displayed if there is no internet connection available.
You should check with javascript on the client side, because the php is server side and will be executed only if is online
<script>
$.ajax({
url: "http://www.google.com",
context: document.body,
error: function(jqXHR, exception) {
alert('offline')
},
success: function() {
alert('ONline')
}
})
</script>
If you got no connection, how should you be able to load the PHP page? You have to check on the client if you got an internet access, e.g. by pinging the server.
Alternatively if you're handling the sites output ('Username already exists' and so on), you could check for
if(answer = "") {
//seems to have no connection
//your code goes here
}

error connecting to mysql database through crazy domains

newbie here! I have made a simple form on my site
warning message
I can't seem to figure out what i've done wrong.
This is the php code:
<?php
$servername = "localhost";
$dbusername = "charityh_root";
$dbpassword = "";
$dbname = "charitydb";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$state = $_POST['state'];
$email = $_POST['email'];
//$password = $_POST['password'];
//$sha1password = sha1($password);
//Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
//Check connection
If ($conn -> connect_error) {
die("Connection failed:" . $conn -> connect_error);
}
Line 17 is $conn = newmysqli($...
any idea guys?
Mysql database wizard
Connecting to database now although when i enter in the details (first name, last name, state and email) i get the message first name can not be left blank...
if (empty($fname)) {
Echo "First name can not be blank. Please press back and correct the
issue";
die();
}
if (empty($lname)) {
Echo "Last name can not be blank. Please press back and correct the issue!";
die();
}
if (empty($state)) {
Echo "State can not be blank. Please press back and correct the issue!";
die();
}
if (empty($email)) {
Echo "Email can not be blank. Please press back and correct the issue!";
die();
}
$sql = "INSERT INTO charityh_database (First_Name, Last_Name, State, Email)
VALUES ('$fname', '$lname', '$state', '$email')";
if ($conn->query($sql) === TRUE) {
echo "Thank You";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close ();
?>
$dbusername = "charityh_root";
$dbpassword = "";
Seems like you are using the same username and password as you were using in your local server. You need to replace them with the username and password crazy domain's.
Since as far as I know Crazy Domain deosn't allows a blank password.
EDIT
Besides that I can see the database name you have mentioned in the codes is "charitydb"; which should be something like charityh_database.(By looking at your recent edit this is a wild guess)
The error indicates your username and password do not have access to the database.
Have you checked the database will allow you to connect, using the username and password?

getting white screen after executing php code

Hi I am working on a simple php registration. But everytime I am submitting the registration page, i am getting a blank screen, no error, no display.
The code in my php file is :
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
$IP = //my hostname
$dbuser = "my user id";
$conn = new mysqli_connect($IP, $dbuser, "","my databse name");
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$address = $_POST['address'];
$query = "SELECT email FROM user where email='".$email."'";
$result = mysqli_query($conn,$query);
$numResults = mysqli_num_rows($result);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address
{
$message = "Invalid email address please type a valid email!!";
}
elseif($numResults>=1)
{
$message = $email." Email already exist!!";
}
else
{
mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
echo $message = "Signup Sucessfully!!";
}
mysqli_close($conn);
}
print_r(error_get_last());
?>
there is no issue in establishing connection as i am using this connection method in other pages and they are working fine. Also i should specify that currently i am working on cloud 9 and my mysql database is on cloud9 itself.
please help me in undersstanding the trouble.
You say your line 42 has the insert query. I suppose you mean this query:
mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
As your error says "2 parameters expected", you are missing here your connection parameter. You should have this:
mysqli_query($conn, "(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
Put this in on the first line of your script
ini_set('display_errors', '1');
This will then display the errors on the page and you will be able to see what is going wrong.
Also rather than the IP address try "localhost"
$conn = new mysqli_connect("localhost", $dbuser, "","my databse name");
If the database is on the same machine this may solve your problem.
Also your code is incredibly unsafe and will suffer from SQL injection. Please consider the following lines:
$email = $_POST['email'];
$query = "SELECT email FROM user where email='".$email."'";
Basically you put whatever the user is passing into your SQL statement. What if he enters "' OR 1; DROP TABLE user". You would lose all your data.
Please read about SQL injection and use PDO:
http://en.wikipedia.org/wiki/SQL_injection
http://php.net/manual/fr/book.pdo.php

PHP register form not connecting to database

I have a simple user registration form and external connection script with some strange results.
The page register.php shows the form fine, however seems to display my entire connection string before the form?
It then throws up errors in relation to my connection variable '$dbcon' (I have commented the line at which this happens) Here is my register.php code:
<?php
session_start();
require "connect.php";
if (isset($_SESSION['username'])){
header("location: members.php");
}
if (isset($_POST['submit']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
$rpass = $_POST['rpass'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
if ($user == "" || $pass == "" || $rpass == "")
{
echo "Please fill all fields";
}
else
{
if ($pass != $rpass)
{
echo "Passwords do not match";
}
else
{
//This is where the errors are found
$query = mysqli_query($dbcon, "SELECT * FROM users WHERE username = '$user' ") or die ("Cannot query table");
$row = mysqli_num_rows($query);
if($row == 1)
{
echo "This username is already taken";
}
else
{
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Cant insert data");
echo "Successfully added user!";
}
}
}
}
?>
And here is my connection file 'connect.php' (the $dbcon string is the one that prints out??)
$server = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'bodgett';
$dbcon = mysqli_connect($server, $user, $pass, $dbname)or die("Can not connect to Server.");
Specifically, the error is 'Notice: Undefined variable: dbcon in C:\webserver...\register2.php'
Can anyone suggest why is doesn't recognize this variable?
Probably a wrong filename (maybe file isn't called connect.php) OR wrong file extension? (html instead of .php)
I just copied all your code, and it works for me. Aswell I don't see php start and closing Tags.
I agree with #Xatenev. Also, you may want to consider using PDO for your database interactions, it's the most secure way. I found this very helpful: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
Sorry if this seems irrelevant, just trying to help.
The connection file 'connect.php' is not enclosed within tags, hence not usable and explains why the text was simply printing out at the top of the page.
Check if mysqli extension is enabled
the code that generates $dbcon is inside a class or inside some function?
If yes, maybe you need to return or call it properly.

Categories