I have a register and log in page that uses php scripts and stores them in a mysql data, but when each member logs in theyre brought to the same members page. How can i make it so that each member is brought to their individual page that only they can see?
This is the members log in page
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Home page</title>
<link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
<p><a href='change-pwd.php'>Change password</a></p>
<p><a href='access-controlled.php'>Your products</a></p>
<br><br><br>
<p><a href='logout.php'>Logout</a></p>
</div>
</body>
</html>
You can do this by using sessions. So when a user logs in. He gets a specific session value for his profile. This session can then be used to bring up specific information for his profile.
Run something like this on login:
<?php
// Start the session
session_start();
// Require the database file
require 'database.php';
// Fetch session_check value when submit is pressed (lets say it's "johnny")
$fetch = $database->prepare("SELECT session_check FROM user WHERE username = $username");
$fetch->execute();
$fetch_session = $fetch->fetchAll();
$user_session = $fetch_session[0]['session_check'];
// Set session variable
$_SESSION["user"] = $user_session;
?>
Echoing this on the members page would then give me and output of: johnny.
<?php
// Start the session
session_start();
// Require the database file
require 'database.php';
// Bind the session variable to a new variable (you should skip this)
$user_session = $_SESSION["user"];
// Fetch personal profile from database
$fetchtwo = $database->prepare("SELECT username FROM user WHERE session_check = $user_session");
$fetchtwo->execute();
$fetch_info = $fetchtwo->fetchAll();
// Display the username depending on user
echo $fetch_info[0]['username'];
?>
Now, presume you have a database with 5 columns. The 5th column name is session_check. Now when you login the value from this column specified by the username used, will be added to the session. When in the members area this exact same session will be used to fetch other information from the same row. This is how you create a dynamic and private profile in PHP.
Read about this here:
w3schools about sessions
PHP sessions
Well, isn't a specific member page just really just the same page with different that is uniq to a member. I think how you could start is by figuring out how to manage the member data which will actually you to create 'different' login pages. You could have data stored in a database. Then you have a php script that reads the data for a member any then creates their login page. So, basically, you still only need one script, but it uses a database.
Related
I have 3 pages have html code similar to just example
<!DOCTYPE html>
<html>
<body>
<div>
html code
</div>
<div>
html code
</div>
<div>
html code
</div>
</body>
</html>
and have 2 php files login.php and logout.php , is it possible to strict access to the 3 pages only to login users
First, Put in header page this session_start();, Ofcourse the header page is included||required in every php page you have.
Second, When the user Login using your Login page, Put the sessions if his data are valid
<?php
if($user && password_verify($password, $user['password'])){
$_SESSION['id'] = $user['id'];
}
?>
In this one we used his id inside session, Now all you have to do is checking if this session is active, If it is not active, You redirect the visitor using header() to the index||404 page like this
<?php
if(!isset($_SESSION['id'])){
die(header("location: 404.php"));
}
?>
and remove the ! for signup & login pages, Since you don't want a logged in user to access the login or register page again.
Third, For logout page, Just put
<?php
session_start();
session_unset();
session_destroy();
header("location: index.php");
exit();
?>
inside it
This answer assumes that you have 3 php files and not html. You need to save those files as .php if you want to manage this using PHP.
Yes you can manage that using a variable or session.
You can redirect the user if they are not logged in. Or, you can show the part of the text and link only if they are logged in.
<?php
if($logged_in) {
?>
Only for logged in users
<?php
}
?>
You need to use session to do it in PHP, it will be like this.
$_SESSION(id)
You can see some tutorial in google.
Refer the below link http://www.makeitsimple.co.in/PHP_loginexmp.php
After browsing through static pages and paying, I'd like for the user to see a confirmation page at .com/confirmation/randomkey. How do I create the random key after payment if I didn't create it before by manually adding a php file to my server. There's no point in creating the URL before since I don't know who's gonna purchase and every single one should be unique.
You can user SEO-Friendly Links in order to rewrite the URL.Then you will be able to handle the randomkey as a parameter and do not need for each user a individual page.
Example:
www.example.com/confirmation/randomkey will be rewritten to www.example.com/?a=confirmation&key=randomkeyThis is often used by MVC: e.g. the first parameter (confirmation) will be the controller and depending on the parameter (randomkey) the contentwill be different. In this case, you will not need static sites.
Okay, I need apologise as I misread your question. I can see one of the answers briefly touched on it so I will explain a little more in depth whilst utilising my answer for the random key.
So a the moment (judging by your comments and question) you have static pages but you would like the confirmation page to be dynamic.
Your best having an index.php in the root of .com/confirmation/. This can have your static confirmation page HTML along with the PHP content.
.com/confirmation/index.php?key=123456
<?php
$key = $_GET['key'] //Get key from URL which 123456
//Check if Key is set or else redirect them somewhere else
if (!isset($key)) {
header('Location: http://www.foo.com/foo.php' );
}
?>
<html>
<head> </head>
<body>
<p> Confirmation Key: <?php echo $key; //Print confirmation key to screen ?> </p>
</body>
</html>
This could also be achieved by passing data using sessions which means no data is passed via URL. This can be picked up in $_SESSION variable which can be accessed from page to page. By doing this, you would NOT need to pass the variable in a URL, you would just need to generate it from the page before confirmation and send the confirmation key. Sessions last until the browser has been closed so it's not like cookies where you can set the expiry of the data.
Page before confirmation
<?php
//Start a session
session_start();
//add to session variable
$key = $_SESSION['key'] = "Your KEY";
?>
.com/confirmation/index.php
<?php
session_start(); //Needs to start before session data can be read
$key = $_SESSION['key'] //Data is queried from server NOT URL.
//Check if Key is set or else redirect them somewhere else
if (!isset($key)) {
header('Location: http://www.foo.com/foo.php' );
}
?>
<html>
<head> </head>
<body>
<p> Confirmation Key: <?php echo $key; //Print confirmation key to screen ?> </p>
</body>
</html>
See:
http://php.net/manual/en/function.session-id.php
I have a code to log in to a database system and do some simple tasks like view user details, edit user details kind of tasks. After logging in, a user should have the ability to log out. My question arise here. I need to redirect to a page which display some text(logoff.php page) and then redirect to the index page. But once I click the logout link it directly goes to the index page which is the login page.
a user is redirected to the log off page from a page and my logoff.php code is this
<?php
session_start();
if (isset($_SESSION["uname"])) {
unset($_SESSION["uname"]);
}
header("Location:../index.php");
exit;
?>
<html>
<head>
<title>Logged Off</title>
</head>
<body>
<br><br>
<div align="">
<h2>You are now logged off</h2>
home</div>
</body>
</html>
It should show the text if a user is redirected to here. But I am sure that it comes to this page since it shows the text inside html tags when I remove the php part of the code. I think the problem is with $_SESSIONcode segment. Doesn't it authenticate the user? Is that the reason why I am redirected to the index page with out listing the content of the logoff page?
My index page which is used for logging is this.
<?php
session_start();
$mess="";
if(isset($_POST["submit"])&&$_POST["submit"]=="Sign in") {
//conncet to the database
require_once("./dbcon/user.php");
include("./dbcon/dbcon.php"); //database connection function
$user=$_POST["uname"];
$password=md5($_POST["password"]);
//retriving data from db
$query = "SELECT user_name FROM user_info WHERE user_name = '$user' AND password ='$password'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
$name=$row["0"];
}
if(mysql_affected_rows()==0) {
$mess = "<font color=purple size=2><b>Wrong username or password.<br>Please try again.</b></font>";
} else {
$_SESSION["uname"]=$name;
header("Location:./user/user1.php");
exit;
}
}
?>
<html>
<!-- html form -->
</html>
In this case do I need to include index.php page into the logoff.php page because SESSION variable is created in the index.php page? Is that the reason or any other?
Thanks!
The answer is in that line, and is a feature, not a bug :
header("Location:../index.php");
This does the redirection server side so that your user never gets to see the page body as he only receives this header, telling the browser to go the that URL.
instead of doing this, you should either use a javascript redirect function with setTimeout() , or use the meta-refresh which is really indicated in this case I guess :
<meta http-equiv="refresh" content="10;URL=yourindexpage.php" />
Note the 10 in the line here above, it indicates the delay in seconds before the redirection occurs.
You can do like this
<?php
session_start();
if (isset($_SESSION["uname"])) {
unset($_SESSION["uname"]);
}
?>
<html>
<head>
<title>Logged Off</title>
</head>
<body>
<br><br>
<div align="">
<h2>You are now logged off</h2>
home</div>
</body>
</html>
Hi there I had some problem with my coding. Can you help me, the situation like this. I created one alert box that asks user if they want to exit from system. I want to do so if users want to logout press yes and the session will be terminated but if they press no the session will not be terminated. This was my coding:
<?php
session_start();
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Logout confirmation</title>
<script type="text/javascript">
var userAns;
userAns = confirm("Click OK to logout or CANCEL to stay at this page");
if(!userAns)
window.location= "baseupdate1.php";
else
window.location= "success.php";
</script>
</head>
<body>
</body>
</html>
Can i ask something, my mini project had admin and user . If user login, it will direct to mainpage.php but if admin login it will go to admin.php. Admin.php can view user register, forms, and comments. I had used session to change button from sign up to userinfo at my header
<strong><?php
if(isset($_SESSION['CurrentUser'])){
echo 'USER INFO';
}else
echo '<a href="signup.php">SIGN UP';
?> </strong>
Instead if has session the link will change, can i make if session admin login, will had no userinfo and signup but link admin info that had user register, forms, and comments. Sorry if my english not good. But i will try to make you all understand about it.
Set session during login
session_start();
$_SESSION['login'] = 'value';
In your success.php page add this
if(isset($_SESSION['login']))
{
unset($_SESSION['login']);
session_destroy();
}
I think it will help you.
My session don't seem to be saved. I have a log in page that saves the users id as a session. But when I leave the page and click on another page the sessions are not saved.
Here are my code. I have a main page called index.php that look like this:
<?php
if(!isset($_SESSION)){
session_start();
}
?>
<!DOCTYPE html> <HTML> <HEAD> </HEAD> <BODY>
<?php
$page_content = 'startpage.php';
if (isset($_GET['link']) && !empty($_GET['link']) ) {
$page_content = $_GET['link'];
}
include "$page_content";
include 'menu.php';
include 'footer.php';
?>
To login I use this page called admin.php. Within this page I have loggin code that works fine.
If the user has written right username and password then this happens:
$_SESSION['user_id'] = $user_id;
print_r($_SESSION);
The code above shows the current sessions variables. But as soon one click on another page the variables are left empty.
At the bottom of index.php are the footer.php page included, and it looks like this:
<?php
if(!isset($_SESSION['user_id']) ){
echo "The sessions are not initiated. ";
}
if (empty ($_SESSION['user_id']) ) {
echo "The sessions are empty. ";
}
echo "<a href='index.php?link=admin.php'>Administration</a>";
?>
In this footer I have put two if questions to check the sessions.
After I have succeded to logg in and clicked on an another page the if questions detect that my session are empty and not initiated.
How should I get the sessions to work?
Perhaps I should add that I use a rent database that use MySql.
Before you can use $_SESSION you should always start it with:
session_start()
Also, session_start() must always be called before you output anything on the page, otherwise it will not work properly