php code and website functioning on localhost but not working on server


php code and website functioning on localhost but not working on server



Good day, my login script which i've been building on my localhost host wamp server is working perfectly well on the localhost but has refused to function well ever since i uploaded it to the server. it reads the username and password and if correct it does nothing but if wrong it alerts me that is it wrong. if correct it is supposed to redirect me to the home page. i've been struggling with it ever since on the server. please any one that will be of help will be much appreciated. it does the same thing too for my register page. it registers but doesnt give any feedback and also does not redirect to the login page but on the localhost everything seems fine
login script


<?php
include 'connection.php';
if(isset($_SESSION['use'])) // Checking whether the session is already there or not if
// true then header redirect it to the home page directly
{
header("Location:home.php");
}

if(isset($_POST['login'])) // it checks whether the user clicked login button or not
{
$user = $_POST['code'];
$pass = $_POST['user'];
if(!filter_var($_POST['code'], FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('invalid Email Address')</script>" ; // Use your own error handling ;)
}

if (!preg_match("/^[a-zA-Z0-9 ]*$/",$pass)) {
echo "<script>alert('Invalid password. Only letters, numbers and white space allowed')</script>" ;
}
// Secure the credentials

// Check the users input against the DB.
$user = stripslashes($user);
$pass = stripslashes($pass);


$check=mysqli_query($conn, "SELECT * FROM userregister WHERE email = '$user' AND password = '$pass'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0){
while( $row = mysqli_fetch_assoc($check) ){
$_SESSION['use']=$row['email'];

header("Location:home.php");
// On Successful Login redirects to home.php

}}

else
{
echo "<script>alert('Incorrect Login details')</script>" ;
header("refresh:0");
}
}
?>
<html>
<head>
<title>Prediction Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="icon" type="image/png" href="2.png">
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="w3.css">
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="index.php">Prediction Home</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">X
</a>
</div>
<div>
<h4>Enter your Log-In details</h4>
</div>
<div class="form">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<label for="E-mail">E-mail</label>
<input type="text" id="E-mail" name="code" required="required">
<label for="">Password</label>
<input type="password" id="" name="user" required="required">
<input type="submit" name="login" value="Log-In">
</form>
<p>Forgot Password? <a class="button" href="forgotpass.php"><span>Click Me!</span></a></p>
</div>

<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>

</body>
</html>



register script


<!doctype html>
<html>
<head>
<title>Prediction Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="w3.css">
<link rel="icon" type="image/png" href="2.png">
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="index.php">Prediction Home</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">X
</a>
</div>
<div>
<h2>REGISTRATION PAGE</h2>
</div>
<div class="form">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<label for="fname">FULL NAME</label>
<input type="text" name="option" required="required">
<label for="Email">E-mail</label>
<input type="text" name="option1" required="required">
<label for="pass">PASSWORD</label>
<input type="password" name="password" required="required">
<label for="pass2">RE-ENTER PASSWORD</label>
<input type="password" name="password2" required="required">
<input type="submit" name="register" value="Register">
</form>
<p>Already Registerd? <a class="button" href="login.php"><span>Sign In</span></a></p>
</div>
<div id="page"></div>
<footer style="background-color: purple ">
<div style="text-align: center">
<h2>Quick Links</h2>
<a style="vertical-align: middle;color: white" href="index.php">Prediction Home</a>
<a style="vertical-align: middle;color: white" href="contact.php">Contact Us</a>
<a style="vertical-align: middle;color: white" href="Store.php">My Store</a>
<a style="vertical-align: middle;color: white" href="basic.php">Basic</a>
<a style="vertical-align: middle;color: white" href="regular.php">Regular</a>
<a style="vertical-align: middle;color: white" href="weekend.php">Weekend</a>
<a style="vertical-align: middle;color: white" href="about.PHP">About</a>
<a style="vertical-align: middle;color: white" href="conditions.PHP">Terms & Conditions</a>

</div>

<div style="text-align: center;">

<h2>Contacts:</h2>
<p style="color: white;vertical-align: middle">Call Us On:08136668621 <br>
MAil:user@predicthome.com</p>
<a href="https:/twitter.com/predicthome" class="fa fa-twitter"></a>
<a href="https://www.instagram.com/predictionhome/" class="fa fa-instagram"></a>
<a href="mailto:user@predictionhome.com" class="fa fa-yahoo"></a>


</div>

<div style="color: yellow;text-align: center;">Prediction Home &copy; <?php echo date("Y");?></div>
</footer>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
<?php
include 'connection.php';
if(isset($_POST['register'])) // it checks whether the user clicked login button or not
{
$option = $_POST['option'];
$option1 = $_POST['option1'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if(!filter_var($_POST['option1'], FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('invalid Email Address')</script>" ; // Use your own error handling ;)
}
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$option)) {
echo "<script>alert('Invalid NAME. Only letters, numbers and white space allowed')</script>" ;
}
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$password)) {
echo "<script>alert('Invalid password. Only letters, numbers and white space allowed')</script>" ;
}
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$password2)) {
echo "<script>alert('Invalid password. Only letters, numbers and white space allowed')</script>" ;
}
if ($password !=$password2) {
echo "<script>alert('Password does not match')</script>" ;
}
// Secure the credentials

// Check the users input against the DB.

$insert=mysqli_query($conn,"SELECT * FROM userregister WHERE email='$option1'");
$insertrows=mysqli_num_rows($insert);
if($insertrows>0 || $password !=$password2|| !preg_match("/^[a-zA-Z0-9 ]*$/",$password) || !filter_var($_POST['option1'], FILTER_VALIDATE_EMAIL) ){
echo "<script>alert('Email already exists or Invalid Email address')</script>" ;
}
else {
$check="INSERT IGNORE INTO userregister(name,email,password) VALUES('$option','$option1','$password')";
if ($conn->query($check) === TRUE) {
header("refresh:0;url=login.php");
echo "<script>alert('registered')</script>" ;


$to = "$option1";
$subject = "Account Registration Successful";
$body = "Hi $option, Welcome to Predict Home. Your Favourite Site to select the best of Games that wll Bring you Good moneyrnWe hope we keep in touch and hear from you more often. rnVisit our site regularly to view our predicted Games from our highly acclaimed analysts.rnOnce again you are Welcome.rnPredict Home Admin";
$lheaders= "From: <user@predicthome.com>";
mail($to, $subject, $body, $lheaders);

}
}


//send email


}
?>
</body>
</html>





You need to use prepared and parameterized queries to protect against sql injection hacks. You should also use the php password lib to securely store passwords as hashes in the database
– JimL
Jun 30 at 7:38




3 Answers
3



Change your PHP version in the server. it must be like your PHP version in the local server





Still not working
– Kelechi Ezumah
Jun 30 at 7:26





what is the error?
– AbdulAhmad Matin
Jun 30 at 7:27





i am not seeing any error
– Kelechi Ezumah
Jun 30 at 7:36





what is your php version in both server localhost and online server?
– AbdulAhmad Matin
Jun 30 at 7:43





localhost-5.6.25 server-5.6.36
– Kelechi Ezumah
Jun 30 at 7:55



I don't have reputation to comment, so I'll try to give an answer.



Check your connection.php file and verify if the hostname, etc are correct.



Most likely it can't connect to the database due to not having it set properly so it'll check for the errors you tell the script to but won't do the database section (querys).





It connects to the database
– Kelechi Ezumah
Jun 30 at 7:26





In the server too ?
– Rodrigo Alves
Jun 30 at 7:30





yes it does connect in the server too but i dont get a response back. if i register a user i stores the details in the database but i dont get a message that it has stored it. if i also log in, it verifies it with the database and if correct it just remains on the page but if not correct it tells me login details wrong
– Kelechi Ezumah
Jun 30 at 7:35





You say in the localhost it is working just as you want ? Well I saw something idk I think you should fetch the data first and then redirect for where you want the user to go instead of trying to trying to redirect inside the while loop (while you fetching)
– Rodrigo Alves
Jun 30 at 7:43





Well @KelechiEzumah you do the redirect before the echo.. Try changing that order. First you echo the alert and then your redirect to the page.
– Rodrigo Alves
Jun 30 at 7:46



Check if you have stored the files on your server in the same way as they are in your localhost folder.



EDIT:


if(isset($_SESSION['use']))
{
header("Location:home.php");
}



What is the variable 'use' here? Looks like you have copied the code and forgot to change the variable name, that is why it is not redirecting to the right page.





yes i stored it correctly. some pages are linking well, but the ones that have to do with php scripts are having issues
– Kelechi Ezumah
Jun 30 at 8:06





Is there any error being shown? Can you explain the "issues" you are talking about?
– Sidharth Soni
Jun 30 at 8:14





its not redirecting to another page when login details are correct or when i try to register but it tells me when the login details are wrong or when ive input a wrong character when trying to register as a user
– Kelechi Ezumah
Jun 30 at 8:52





@KelechiEzumah please check the edit in my answer
– Sidharth Soni
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

.

How to generate build files in next js

Delphi Android file open failure with API 26