User home page php

PHP Login System

Following up on yesterday’s introduction to PHP Sessions, let’s talk about building a simple login system in PHP. To start, you will need to make sure that pages on your site that are behind the login page will have sessions enabled. This can be done for all of your pages by setting the php.ini variable session.auto_start or making sure to include session_start() at the top of your pages. For this walkthrough we’ll be creating a small site that had 4 pages: main index (the home page), login and logout pages and a restricted access page. For the sake of simplicity we’ll be using a hardcoded username and plaintext password. For obvious reasons we don’t recommend doing so and at the very least you should be encrypting the password.

To start off, let’s put together our home page index.php :

 // Starts the session session_start(); // Checks if the user is logged in if (isset($_SESSION['username']) && $_SESSION['username'] == 'admin'))  echo 'Hello administrator, would you care to access the secret page or logout?'; > else  echo 'Hello stranger, if you the admin you could login.'; > ?> 

Nothing fancy, we start the session and check to see if the username is set in the session. If it is, give some secret options, if not, present the user with the opportunity to login. Speaking of logging in, on to the login page, login.php :

 // Starts the session session_start(); // Checks if the user is logged in if (isset($_SESSION['username']) && $_SESSION['username'] == 'admin'))  // User is already logged in, send them home! header('Location: /'); exit; > else  // Checks if the user is trying to log in if (isset($_POST['username'], $_POST['password']))  if (trim($_POST['username']) == 'admin' && trim($_POST['password']) == 'abc123')  $_SESSION['username'] = 'admin'; // Successful login, send them home! header('Location: /'); exit; > else  echo 'Invalid login credentials, try again.'; > > // Displays the login form ?>  method="post" action="/login.php">  for="username">Username:  type="text" id="username" name="username">  for="password">Password:  type="text" id="password" name="password">  type="submit">  <?php > ?> 

A bit more going on in this one, out of the gate we start our session and then check to see if the user is already logged in. If they are logged in, we send them back to the home page as there’s no reason for them to be logging in again. After that, we check to see if the login credentials have been supplied and check if they are valid. If they are not, we display an error, if correct send them back to the home page as they are now properly authenticated. Lastly we display the login form which is set up to post back to itself, that’s why all of the login logic exists in the same file. You could very well abstract out the login logic to it’s own file, perhaps login/process.php or something similar. As mentioned before, the username and password are hardcoded and it is not recommended for use outside of this example. The check if the information is correct could be swapped out to check a database or other datastore as well as hashing the password as you should never store plaintext passwords.

Now that you have out page to log the user in, let’s set up our “secret” page named secret.php . The page is very similar to our main index with the difference that unauthenticated users will be will served an HTTP Error Code (401 Unauthorized) instead of a message to log in.

 // Starts the session session_start(); // Checks if the user is logged in if (isset($_SESSION['username']) && $_SESSION['username'] == 'admin'))  echo 'Welcome to the super secret page!'; > else  header("HTTP/1.0 401 Unauthorized"); > ?> 

Now we have a page that’s completely locked out to unauthorized users. Last but not least, we need to write a log out script that will destroy the session. This file will be named logout.php and will be our shortest script because it doesn’t have any markup. To keep things simple we won’t check if the user is logged in or not, just destroy the session and redirect:

 // Starts the session session_start(); // Destroys the session session_destroy(); // Kicks the user home header('Location: /'); ?> 

That’s it, you now have a simple site that has a login system! Tomorrow’s post will be a continuation of PHP Sessions by delving into some advanced configuration options.

Good stuff? Want more?

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Источник

Php url redirect to home page

Question: I am using this redirect code for redirection from login page to home page but 1—>directory/includes/code.php 2->directory/homepage.php how to redirect from 1 to 2 Solution 1: Try this: Solution 2: You can do relative redirect using: Question: I have a link in my page( ). In the end solved it with: In place of the header and exit rows above.

Php url redirect to home page

Well, I don’t know if I made a title properly but I’ve got a lil php script out there and I am wondering if there might be possibility when user go to this url:

siteurl.com/index.php?token=J55OSGTTk3W7mRcuq54006w7ROv 

to get automatically only /index.php without the content that he copy/pasted in this case

?token=J55OSGTTk3W7mRcuq54006w7ROv 

Hope it is possible. Cheers.

Yes. Issue a Location header and use parse_url() to get the parts you need.

$url = "http://siteurl.com/index.php?token=J55OSGTTk3W7mRcuq54006w7ROv"; $parsed = parse_url($url); $redirect = $parsed["scheme"] . "://" . $parsed["host"] . $parsed["path"]; header("Location: $redirect"); 

Maybe explode($url,»?»)[0] . If there’s no question mark, then it should return an array of length one. If there is, you’ll only get everything before the ‘?’.

WordPress: Redirect homepage and pages to account page when,

How to redirect a page in PHP

Need Help Or Need code? Feel Free To Contact Us Here http://www.noblecomputer.co.in/suppor
Duration: 2:52

PHP — Redirect Web Page

You can use PHP to redirect visitors to your web page to different pages automatically. This
Duration: 8:16

Redirecting to another page using PHP: how, why and best practices

Access the full course ➤ https://davehollingworth.net/mvcauthyHow to redirect to another URL Duration: 5:16

Login and redirect to user home page

The problem I can’t currently solve is when a user logs in. They arrive at the site (index.php) and enter username and password, which gets submitted via a Post form back to index.php — if there are incorrect details then they get an error message. But if successful then I would like them to be taken to their user home page — but I can’t do this! I am left presenting them with a link to the home page, which is more than a little clunky.

Seems there must be an obviously solution — never seen a site before that didn’t redirect! Not sure is the answer is PHp, HTML, or Javascript.

Sorry if the question wasn’t up to scratch — still learning here. Have tried to put the redirect in an if statement but still won’t work. I think ideally the redirect would be top of the code, but can’t see how to do that and retain the login procedure? Code as follows:

if (isset($_POST['user'])) < $user = sanitizeString($_POST['user']); $pass = sanitizeString($_POST['pass']); $salt = "randomsalt"; $md5pass = md5($salt . $pass); if ($user == "" || $pass == "") < $error = "Not all fields were entered
"; > else < $query = "SELECT username,password FROM users WHERE username='$user' AND password='$md5pass'"; if (mysql_num_rows(queryMysql($query)) == 0) < $error = "Username/Password invalid
"; > else < $_SESSION['user'] = $user; $_SESSION['pass'] = $pass; header("Location: http://localhost:8888/profile.php?view=$user"); exit; >>

Thanks for your help guys — still couldn’t resolve then need to have the re-direct at the top of the page, but equally having to have my login script in place. In the end solved it with:

In place of the header and exit rows above.

You are probably redirecting wrong. First, put this at the very top of index.php :

This is a simple redirect with an exit statement to stop all processing afterwards, so you can just test to see if the redirect is working. If it works, then add a condition to the redirect, so something like

but, of course replace /*login credentials are valid*/ with a call to a function or whatever that actually checks the credentials and returns a boolean value.

this solution is for php, hope this will help you.

$user_name = $_POST['username']; // posting value through form $pass_word = $_POST['password']; // posting value through form $sql = mysql_query("select 1 from admin_users where username='".$user_name."' and password='".$pass_word."'"); // checking username and password in database if ($sql) else

take username and password in a variable.

$username = $_POST['username']; $pwd = $_POST['password']; 

Compare this with your table like below:

$sql = mysql_query("select * from tbl_users where username='".$username."' and password='".$pwd."'"); if(mysql_num_rows($sql)>0)< header("Location:homepage.php"); >else

Redirecting to another page using PHP: how, why and best practices, Access the full course ➤ https://davehollingworth.net/mvcauthyHow to redirect to another URL Duration: 5:16

Folder one level up PHP header redirect

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']).'/homepage.php'; header('Location: '.$home_url); 

I am using this redirect code for redirection from login page to home page

how to redirect from 1 to 2

$home_url = '../homepage.php'; header('Location: '.$home_url); 

You can do relative redirect using:

location = '../'; //one level up location = '/path'; //relative to domain 

If session exists redirect to other page PHP, home.php:

How to use php header() to redirect to homepage?

I have a link in my dashboad page( http://localhost/dashboard.php ). The link is as following:

In the logout.php file, I have the following code:

So clicking on that link the user agent is redirected to http://localhost/ . I want to redirect to http://localhost without the trailing slash. My question is

  1. Why does header(«Location: «); not work?
  2. Why does header(«Location: ../»); still lead to http://localhost/ .
  3. Why does header(«Location: /../»); doesn’t remove the trailing slash?

P.S: I have read the official docs https://www.php.net/manual/en/function.header.php and other SO posts but didn’t find any explanation.

You can use absolute url header(«Location: http://localhost»);

  1. Because you need to specify where do you want to redirect
  2. Because http://localhost and http://localhost/ are equal
  3. Because there is no upper level then /

Источник

Читайте также:  Call cmd in java
Оцените статью