get variables in php - 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']

Related

Email service providers PHP filter

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');
}
?>

Not redirecting to home page (index.php)

Is there something wrong about my code? It works but its not redirecting to my index.php it always ended up in the login.php where the form is located.
<?php
include 'core/ini.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[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
thanks!
EDIT *
this is my login.php
<?php
include 'core/ini.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[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
this is where the process go. I don't know where should I put my start session but I don't know why it works without having an error.
change header('Location :index.php'); to header('Location: index.php'); That space might be the cause.
I guess you missed the session_start(); on top of the page since you are storing session. Initiate the session_start();.
Also does your login() function returns TRUE? Echo something to check whether the function returns TRUE as expected.
You hae to use session_start on top of page and I think you should remove exit after headerlocation..

How to check is someone is online?

So i have made a simple log in system in php how would i go on making a function on php to check if someone is online also can it be done in php
login system
<?php
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'Please Enter a username and password';
} else if (user_exists($username) === false) {
$errors[] ='Sorry but the user you entered dose not exist';
} else if (user_active($username) === false) {
$errors[] ='You have not activated your account please check your email to activate your account';
} else {
if (strlen($password) > 32){
$errors[] ='Passwords to long !';
}
$login = login($username, $password);
if ($login === false) {
$errors[] ='incorrect username or password';
} else {
$_SESSION['user_id'] = $login;
header('Location: index.php');
exit();
}
}
} else {
$errors[] = 'No information received';
}
echo output_errors($errors);
?>
You can use sessions for this:
http://php.net/manual/en/book.session.php
Simple usage of sessions:
// Start the session
if(!session_id())
session_start();
$_SESSION["the_user"] = true;
// To check if the session is alive
if(!empty($_SESSION["the_user"]))
echo "User is online !";
else
echo "User isn't online!";
// Delete the session
unset($_SESSION["the_user"]);
Note that this is just a simple usage of the session, the session will be alive even if the user went of the site. but it will be for a few minutes. (session's expire time)

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.

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