Email service providers PHP filter - php

I want to make custom path for "gmail-hotmail-yahoo" users for example
<input type="email" name="email"/>
now I want simple PHP code to filter email service providers
if my email is HelloWorld#gmail.com go to /gmailuser path
example
<?php
$email = $_POST['email'];
if ($email == 'gmail email' ) {
header('Location: /gmailusers');
} elseif ($email == 'hotmail email') {
header('Location: /liveusers');
} else {
header('Location: /unknownusers');
}
?>

Use stripos to find (without case) if the email contains the keywords you need to redirect your users:
<?php
$email = $_POST['email'];
if (stripos($email, '#gmail.com') !== false) {
header('Location: /gmailusers');
} elseif (stripos($email, '#hotmail.') !== false) {
header('Location: /liveusers');
} else {
header('Location: /unknownusers');
}
?>

Related

How do i restrict my sign up to people with a specific email domain in PHP

How do i restrict my sign up to people with a specific email domain in PHP
So for example, only people with a '#amn.com' email address can register
Use the following regular expression:
<?php
$email = "mytestemail#amn.com";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die("Not a valid e-mail address!");
} else {
if(empty(preg_match("/#amn.com$/", $email))) {
die("E-mail must end with #amn.com!");
} else {
//valid//
}
}
?>
<?php
$email = $_POST['email'];
$domain = explode('#',$email)[1];
if($domain != 'amn.com')
{
die('This domain is not allowed to register')
}
On your server, check the mail input field if it contains the string and ends with it, like this:
$email = $_POST['email'];
$domain = explode('#',$email)[1];
$emailErr = '';
$domain = explode('#',$email)[1];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $domain != 'amn.com') {
$emailErr = "Invalid email";
} else {
//valid - use input
}
For js validation, you can use something like this:
function validateEmail(email)
{
var splitted = email.match("^(.+)#thisdomainonly\.com$");
if (splitted == null) return false;
if (splitted[1] != null)
{
var regexp_user = /^\"?[\w-_\.]*\"?$/;
if (splitted[1].match(regexp_user) == null) return false;
return true;
}
return false;
}

get variables in php

I have this code my friend sent me through a tutorial for a project;
<?php include 'includes/menu.php'; ?>
<h1>Activate your account</h1>
<?php
if (isset($_GET['success']) === true && empty ($_GET['success']) === true) {
?>
<h3>Thank you, we've activated your account. You're free to log in!</h3>
<?php
} else if (isset ($_GET['email'], $_GET['email_code']) === true) {
$email =trim($_GET['email']);
$email_code =trim($_GET['email_code']);
if ($users->email_exists($email) === false) {
$errors[] = 'Sorry, we couldn\'t find that email address';
} else if ($users->activate($email, $email_code) === false) {
$errors[] = 'Sorry, we have failed to activate your account';
}
if(empty($errors) === false){
echo '<p>' . implode('</p><p>', $errors) . '</p>';
} else {
header('Location: activate.php?success');
exit();
}
} else {
header('Location: index.php');
exit();
}
?>
Basically it activates your acc based on email and email code obviously, the email is; test#test.com and code is; code_536203131f86d3.07314347 how would i activate. Something like domain.com/activate?code_536203131f86d3.07314347 or?
PHP's $_GET variables are sent through the HTTP request's query string. So, your URL would be something like this:
domain.com/activate.php?email=test#test.com&email_code=code_536203131f86d3.07314347
Your two variables would then be accessible through the superglobal:
$_GET['email']
$_GET['email_code']

handling errors on my login page

my error handling does not seem to work when i click the submit button without entering any of the fields,i can still login what exactly am i missing ?? its seems my validation is not working,
this is my code below
login.php
<?php
include 'core/init.php';
if (empty($_post) === false) {
$username = $_post['username'];
$password = $_post['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password';
} else if (user_exists($username) === false ) {
$errors[] = 'Username not found';
} else if (user_active($username) === false ){
$errors[] = 'You haven\' activated your account';
} else {
if (strlen($password) > 32){
$errors[] = 'Password too long';
}
$login = login($username, $password);
if($login === false){
$errors[] = 'That username/password combination is incorrect';
} else {
$_session['user_id'] = $login;
header('location: index.php');
exit();
}
}
} else {
$errors[] = 'No data recieved';
}
include 'includes/overall/header.php';
if (empty($errors) === false){
?>
<h2> we tried to log you in, but...</h2>
<?php
echo output_errors($errors);
}
include 'includes/overall/footer.php';
?>
my init.php
<?php
session_start();
require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
$errors = array();
?>
general.php
<?php
function sanitize($data){
return mysql_real_escape_string($data);
}
function output_errors($errors) {
return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}
?>
Use $_POST instead of $_post, they both are different variable, as php is case-sensitive for variable names.

FILTER_VALIDATE_EMAIL not working

I certainly must be missing something here. For some reason filter_var is not working. I'm trying to validate an email from $_POST, and it returns false even with valid emails. But, when I hardcode an email, it works fine. What's wrong?
Here's my php code:
function redirect() { //redirecting to home page function. Used in one of the lectures.
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/index.php");
exit;
}
try
{
$dbh = new PDO($db, $dbuser, $dbpassword);
}
catch (PDOException $e)
{
echo "Connection failure: " . $e->getmessage();
}
if (!isset($_POST['email']) || !isset($_POST['password1']) || !isset($_POST['password2'])) {
redirect();
}
$password1 = htmlspecialchars($_POST['password1']);
$email = htmlspecialchars($_POST['email']);
$password2 = htmlspecialchars($_POST['password2']);
//preg_match('/.+#.+\./', $email) == FALSE
if ($email = "") {
print "email not there";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
print "not real email";
} elseif (strlen($password1) < 6) {
print("password too small");
} elseif (!(preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $password1))) {
print "numbers and letters plz";
} elseif ($password1 != $password2) {
print "passwords not same";
//redirect();
}
Change the first email check:
if ($email == "") {
print "email not there";
}
It is getting the value " instead of checking for it.

We had problems activating your account

When email activate this error message "We had problems activating your account"
any problems my activate code..???
code give below..
'activate.php'
<?php
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
<h2>Thanks, we've activated your account....</h2>
<p>You're free to Log in!</p>
<?php
} else if (isset($_GET['email'], $_GET['email_code']) === true) {
$email = trim($_GET['email']);
$email_code = trim($_GET['email_code']);
if (email_exists($email) === false) {
$errors[] = 'Oops, something went wrong and we could\'t find that email address';
} else if (activate($email, $email_code) === false) {
$errors[] = 'We had problems activating your account';
}
if (empty($errors) === false) {
?>
<h2>Ooops...</h2>
<?php
echo output_errors($errors);
} else {
header('Location: activate.php?success');
exit();
}
} else {
header('Location: index.php');
exit();
}
include 'includes/overall/footer.php';
?>
Email link code given below:
*'user.php'*
The activate function returns false. Make sure you call it in the correct way and that it works as expected.

Categories