Ok so what I am trying to do is create a php variable called "text", set it into a specific error message when program runs into a problem, and redirect the user to the login page and ask them to enter their credentials again with the error message printed in the button.
This is createpage.php which is page where it takes the user input. This page also prints the message.
<?php
include('create.php');
// Includes Login Script
?>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$("#header").load("header.html");
$("#footer").load("footer.html");
});
</script>
<title>Mases Krikorian Website</title>
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" type="text/css" href="login.css">
<style>
h2{
font-family: "Arial Black", Gadget, sans-serif;
}
td{
padding-left: 5px;
padding-right: 5px;
}
</style>
<div id="header"></div>
<div class="bgpic"></div>
</head>
<body>
<div class="container">
<div id="content">
<form method="POST" action="create.php">
<h1>Registration Form</h1>
<div>
<input name="username" placeholder="Username" type="text" id="username">
</div>
<div>
<input name="password" placeholder="Password" type="password" id="password">
</div>
<div>
<input name="email" placeholder="Email" type="text" id="username">
</div>
<div>
<div>
<span><?php echo $text; ?></span>
</div>
<a href="/login.php">Login</a>
<input id="submit" type="submit" name="submit" value="Sign Up">
</div>
</form><!-- form -->
</div>
</div>
</body>
<div id="footer"></div>
</html>
This is my create.php page which sets the "text" variable based on their input, and also makes connection with my mysql database.
<?php
session_start();
define('DB_HOST', '<im putting my host here>');
define('DB_NAME', '<im putting my db name here>');
define('DB_USER','<im putting my username here>');
define('DB_PASSWORD','<im putting my password here>');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$text= '';
function NewUser()
{
$username= $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
if($data)
{
$text= "Account created.";
header('Location: login.php');
$text= "Account created.";
}
}
function SignUp()
{
if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error());
$q = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]'");
if(!$row = mysql_fetch_array($q))
{
newuser();
}
else
{
$text= "Account already exists under that username";
header('Location: createpage.php');
$text= "Account already exists under that username";
}
}
else {
$text= "Account already exists under that username";
header('Location: createpage.php');
$text= "Account already exists under that username";
}
}
if(isset($_POST['submit']))
{
SignUp();
}
?>
Note that the page does the redirecting from the "header" call properly but does NOT set the text. Also note that all connections with the database work properly. Thanks for any help.
header("Location: createpage.php?error=".urlencode($text));'or persist with a session variable, or you do a query string with an error number that you can call from your other page.mysql_queryinterface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way explains best practices. Your user parameters are not properly escaped and there are SQL injection bugs that can be exploited.