/    Sign up×
Community /Pin to ProfileBookmark

how should i proceed to redirect user after successful registration

hi, i am new to programming and here i have some sample of codes that i am working with.

register.php

`/ Change this to your connection info.
$DATABASE_HOST = ‘localhost’;
$DATABASE_USER = ‘root’;
$DATABASE_PASS = ”;
$DATABASE_NAME = ”;
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
exit(‘Failed to connect to MySQL: ‘ . mysqli_connect_error());
}
// Now we check if the data was submitted, isset() function will check if the data exists.
if (!isset($_POST[‘username’], $_POST[‘password’], $_POST[’email’])) {
// Could not get the data that should have been sent.
exit(‘Please complete the registration form!’);
}
// Make sure the submitted registration values are not empty.
if (empty($_
POST[‘username’]) || empty($_POST[‘password’]) || empty($_POST[’email’])) {
// One or more values are empty.
exit(‘Please complete the registration form’);
}
if (!filter_var($_POST[’email’], FILTER_VALIDATE_EMAIL)) {
exit(‘Email is not valid!’);
}
if (preg_match(‘/^[a-zA-Z0-9]+$/’, $_
POST[‘username’]) == 0) {
exit(‘Username is not valid!’);
}
if (strlen($_POST[‘password’]) > 20 || strlen($_POST[‘password’]) < 5) {
exit(‘Password must be between 5 and 20 characters long!’);
}
// We need to check if the account with that username exists.
if ($stmt = $con->prepare(‘SELECT id, password FROM accounts WHERE username = ?’)) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param(‘s’, $_POST[‘username’]);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
// Username already exists
echo ‘Username exists, please choose another!’;
} else {
// Username doesnt exists, insert new account
if ($stmt = $con->prepare(‘INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)’)) {
// We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
$password = password_hash($_
POST[‘password’], PASSWORD_DEFAULT);
$stmt->bind_param(‘sss’, $_POST[‘username’], $password, $_POST[’email’]);
$stmt->execute();
echo ‘You have successfully registered, you can now login!’;

} else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo ‘Could not prepare statement!’;
}
}
$stmt->close();
} else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo ‘Could not prepare statement!’;
}
$con->close();`

and below i will post the home.php file on which i want the user to land and be logged in. registration is working and logging in too. simply after registering it doesnt redirect to home page and stay on the page with the “You have successfully registered, you can now login”.

to post a comment
Full-stack Developer

3 Comments(s)

Copy linkTweet thisAlerts:
@payitforwardauthorAug 27.2022 — seems like i cant edit my post so no home.php code.
Copy linkTweet thisAlerts:
@payitforwardauthorAug 28.2022 — solved with a simple 'a' tag added after the successful registration so the users can click and redirect themselves to the login page. this is basic but i am going to use that for now.. thank you myself hehehe.
Copy linkTweet thisAlerts:
@NogDogAug 29.2022 — In case you want to update this at some time, probably the two most common ways are either via a [PHP header() request](https://www.php.net/manual/en/function.header.php), or via a meta tag in the head section of the HTML, e.g. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#examples
×

Success!

Help @payitforward spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...