Php home page redirect blank - javascript

I have an issue with this code. when I login it takes it does not take me to the home page but stay on the login page. I want to redirect users to (http://localhost/trial/index.php#Home) When successfully logged in. How to fix that?
<!-- Including header file which contains j query and other libraries -->
<?php include("inc/incfiles/header.inc.php"); ?>
<?php
//Check if user is logged in or not
if (!isset($_SESSION["user_login"])) {
//Verification
}
else
{
//Do nothing
}
?>
<?php
//Login Script
//user Login code
//Check user info when user inputs login information
if (isset($_POST['user_login']) && isset($_POST['password_login']) )
{
//filters input info
$user_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['user_login']);//filters everything but numbers and letters
$password_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['password_login']);//filters everything but numbers and letters
$password_login_md5 = md5($password_login); // encrypt password input because password in database is already encrypted in md5
//use Binary for case sensitive option
$sql = mysqli_query($con, "SELECT * FROM users WHERE BINARY username= BINARY'$user_login' AND password='$password_login_md5' AND closed='no' LIMIT 1"); //query
//check for existence if user exists
$userCount = mysqli_num_rows($sql); //Count the number of rows returned
//if username exists start session
if($userCount==1)
{
while($row = mysqli_fetch_array($sql)) //fecthing the row to display information
{
$id = $row["id"]; // store user id into variable called $id
}
$_SESSION["id"] = $id;
$_SESSION['user_login'] = $user_login;
$_SESSION["password_login"] = $password_login;
echo "succes!";
header("Location:#Home");
//exit("<meta http-equiv=\"refresh\" content=\"0\">");
}
else{echo"That information is incorrect, Please try again!";}
exit();
}
?>
<!-- The welcome page where users must provide login info in order to be logged in -->
<div data-role="page" id="Welcome">
<div role="main" id="loginform">
React now
<form action="" method="POST"> <!--provide username and password then submit -->
<input name="user_login" size= "25" type="text"><br /><br /><!-- Enter username /include username placeholder later-->
<input data-clear-btn="false" name="password_login" size= "25" type="password"><br /><br /><!-- Enter password /include password placeholder later-->
<input name="login" value="Login" type="submit" data-theme="a"/><!-- submit button style it later -->
</form>
<div>
Sign Up <!--Redirect user to sign up page if user not member yet-->
</div>
</div>
<div data-role="footer" data-position="fixed" data-theme="a"><!-- Footer display/ displays once-->
<h4>(C) 2016</h4> <!-- copyright symbols include later-->
</div>
</div> <!-- End of the login page-->
<!-- Sign up page where to allow users to sign up-->
<div data-role="page" id="Sign Up">
<div data-role="header" data-theme="a">
<h1>Sign Up</h1>
</div><br><br><br><br>
Sign Up for Reactr
<form>
<!-- Just left the form blank for the moment to make the code smaller and easy to read-->
</form>
<div role="main" class="ui-content">
</div>
</div><!-- End of the sign up page-->
<!-- HOME PAGE AND USER PROFILE PAGE where users can enter and submit texts-->
<div data-role="page" id="Home">
<div data-role="header" data-theme="a"><!-- Jquery settings ref included in the header file-->
<h1>Text</h1>
</div>
<!-- Allow users to search for posted texts-->
<div class="search_box">
<!-- Setting form to allow users to type text, send and search for texts-->
<form action="search.php" method="GET" id="search"><!-- Search form -->
<input type="text" name="q" size="60"/><!-- Search for text /include search placeholder later-->
</form>
</div>
<div role="main" class="ui-content">
Enter your Text<br><!-- Enter and send text -->
<input name="text-basic" id="text-basic" value="" type="text">
Send<!-- submit button with onclcick function -->
</div>
</div><!-- End of the Home page-->
</body><!-- End code-->
</html>

You have at least two problems:
1 - Most importantly, you are attempting to redirect after sending some content with PHP, which is not allowed:
echo "succes!";
header("Location:#Home");
Remove the echo line to fix this.
2 - You also need to update your redirect to redirect somewhere server-side. PHP doesn't know about client-side directives, such as named anchors on a page (in your case #Home)
header("Location:#Home");
Update this to header("Location:index.php#Home"); to resolve it. Note that if your index.php IS your home page, you may not need the #Home at all.

The # symbol in your redirect causes the issue of staying on the same page - since this indicates that its an anchor location in the page you are already in. you need to change it to:
header("Location: index.php#Home");
also - if you are simply trying to redirect your user to the top of the home page - you don't need it at all - you would only try to take them to an anchor that exists further down the page. If what you are trying to direct them to is at the top of hte page just use the relative path with no anchor listed:
header("Location: index.php");

Related

How redirect php Window.location.href or Window.open () methods in php and JavaScript

I follow up a tutorial to learn more about php, in it's source code there is something which seems works at that time but not anymore. here is the code , please let me know what should i change in the code in order to make login process work (currently after entering a valid user name and pass and clicking login it freezes and show first page and not go to home.php
here is template/header.php:
<div class="container">
<!--Head wrap starts-->
<div id="head_wrap">
<!--Header starts-->
<div id="header">
<img src="images/logo.png" style="float:left;"/>
<form method="post" action="" id="form1">
<strong>Email:</strong>
<input type="email" id="email" name="email" placeholder="Email" required="required" />
<strong>Password:</strong>
<input type="password" id="pass" name="pass" placeholder="****" required="required"/>
<button type="submit" id="login">Login</button>
</form>
</div>
<!--Header ends-->
</div>
here is login.php
<?php
session_start();
include("includes/connection.php");
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con,$_POST['email']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$get_user = "select * from users where user_email='$email' AND user_pass='$pass'";
$run_user = mysqli_query($con,$get_user);
$check = mysqli_num_rows($run_user);
if($check==1){
$email = mysqli_real_escape_string($con,$_POST['email']);
$_SESSION['user_email']=$email;
echo "<script>window.open('home.php','_self')</script>";
}
else {
echo "<script>alert('Passowrd or email is not correct!')</script>";
}
}
?>
please note i have tried
echo "<script> window.location.href = 'home.php';</script>";
instead of
echo "<script>window.open('home.php','_self')</script>";
and still doesn't work, since it's tutorial and i have search through stackoverflow can't find any answer i appreciate your help.
This is your HTML code but with a submit button. You say all files are located in the same folder so this should work. I did not make any changes to login.php but it should run when the page is submitted.
<div class="container">
<!--Head wrap starts-->
<div id="head_wrap">
<!--Header starts-->
<div id="header">
<img src="images/logo.png" style="float:left;"/>
<form method="post" action="login.php" id="form1">
<strong>Email:</strong>
<input type="email" id="email" name="email" placeholder="Email" required="required" />
<strong>Password:</strong>
<input type="password" id="pass" name="pass" placeholder="****" required="required"/>
<input type="submit" id="login" name="login" value="Login">
</form>
</div>
<!--Header ends-->
</div>
</div>
Edit: I can't debug your entire project but after looking over some things I see you are not using the 'name' attribute. When a page is submitted a name/value pair is sent in the $_POST array. If you have no 'name' attribute nothing is sent. Start by adding the 'name' attribute. I have modified the above HTML code to show you how.
You have to use header(...) function but don't forget that your page keep to run at the end. Don't forget to use with die to stop your script. ;)
die(header("Location: home.php"))
or after 5 seconds :
header("refresh: 5; url=home.php");
if($check==1){
$email = mysqli_real_escape_string($con,$_POST['email']);
$_SESSION['user_email']=$email;
return 1;
}
else {
return 0;
}
and javascript check status 1 and 0 then window.location.href and window.open use
Check in your file..
1) header() must be called before any actual output is sent, either by
normal HTML tags, blank lines in a file, or from PHP
2) Combine all your PHP codes and make sure you don't have any spaces
at the beginning of the file.
3) after header('location: home.php'); add exit();
4) after sesssion_start() add ob_start();

php form submission only once per session

I am looking to enable only one submission of form per session. I have tried to disable submit button but this on click function is nothing for bots so extra layer of single submission per session is what i think can save somewhat from bots
OR
Create token for each submission to make submission more secure and unique
Which one is better and how to implement so any user (bots) can not submit same form twice
Code I have is
<form role="form" method='post' action='index.php' id='cme'>
<input type="hidden" name="val" value="<?php echo $val ?>" />
<fieldset>
<div class="form-group">
<center><div class="g-recaptcha" data-sitekey="sitekey"></di</center>
</div>
<div class="row">
<center>
<input type="submit" name="claim" class="btn btn-lg btn-success btn-block" value="Claim Now" id="claim" onclick="setTimeout(disableFunction, 1);">
</center>
</div>
</fieldset>
</form>
Submit section
if(isset($_POST['claim'])) {
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
# Use the recaptcha function here
$resp = getGoogleRecaptcha();
if($resp['success']) {
header('Location: index.php');
# Capture value from the form submit
$bonval = $_POST['bonval'];
# Insert normally
$db->fetchVal("insert into log (`user_id`,`amount`) values (?,?)", array($id, $bonval));
}
}
else { ?>
<div class="overlay"><div class="popup" style="background:red;">
<h2>Opps</h2>
<a class="close" href="#">×</a><br/>
<div><center><span class="blink_me">You missed it</span></center></div>
</div></div>
<?php }
}
Now issue is form opens on popup and user keeps clicking and score keeps adding as with each click session view is +1
Can you please guide me about this issue solving so one click and only one submission
I think it can be better to do form submission through javascript so on submit function can be controlled more wisely to kill popup on more than one submit click....am i right, if yes plz guide this way
Below is one way of doing this using sessions.
Basically I am generating a token and storing it in session if its not already in session. I am also including the same in the form, which can be cross checked (in index.php) to see if it matches with the session variable or not.
<?php
if(! isset($_SESSION['cme-token']) ){
$cme_token = rand(11111, 99999);
$_SESSION['cme-token'] = $cme_token;
} ?>
<?php if (!isset($_SESSION['cme'])){ ?>
<form role="form" method='post' action='index.php' id='cme'>
<input type="hidden" name="token" value="<?php echo $cme_token ?>" />
<fieldset>
<div class="form-group">
<center><div class="g-recaptcha" data-sitekey="sitekey"></di</center>
</div>
<div class="row">
<center>
<input type="submit" name="claim" class="btn btn-lg btn-success btn-block" value="Claim Now" id="claim">
</center>
</div>
</fieldset>
</form>
<? } else {
<div>ALREADY CLAIMED!</div>
}?>
In index.php you can do additional check just to be sure that the token was not modified in between by user like below.
// INDEX.PHP
<?php
if($_POST['token'] == $_SESSION['cme-token']) {
//PROCESS THE FORM
} else {
// IGNORE THE FORM SUBMIT AS IT DOESN'T CARRY PROPER TOKEN
}
?>

Handle HTML <input> Attribute [duplicate]

This question already has answers here:
How to get input field value using PHP
(7 answers)
Closed 6 years ago.
I am writing basic user register page,
My register page contain : usermail ,password, plan type.
There are three plans for plan type.
Three plans are: basic, sliver and gold.
The register_main.php is to store user information in Mysql.
I met issue that is when I click basic or sliver or gold plan, the page will go to register_main page .
I want sent user information only to server ,when they click sign in.
Can anyone help me to solve this issue?
HTML Code:
<html>
<head>
<title>Register</title>
<meta charset="UTF-8">
<!-- Include JS File Here -->
<script src="Script/register_validate.js">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
</script>
</head>
<body>
<form name="registerform" action="register_main.php" method="post" onsubmit="return validate()">
<p id="benefits_text" class="white size_8">Benefits:</p>
<input id="type_basic" type ="image" name="basicPlan" " src="basic.png">
<input id="type_silver" type ="image" name="silverPlan" " src="silver.png">
<input id="type_gold" type ="image" name="goldPlan" " src="gold.png">
<div id="userinfo_content">
<p id="email_text" class="white size_8">Email Address</p>
<input id="email_input" name="userEmail" type="text"class="sign_input">
<p id="password_text" class="white size_8">Password</p>
<input id="password_input" name="password" type="password">
<p id="confirmPW_text" class="white size_8">Confirm Password</p>
<input id="confirmPW_input" name="confirm_password" type="password">
<input id="btn_signin" type ="image" alt ="submit" src="signin.png">
</div>
</form></body>
</html>
register_main.php
include ("config.php");
require ("encrypt.php");
session_start ();
if ($_SERVER ["REQUEST_METHOD"] == "POST") {
// Get userEmail from client side and create a legal SQL string that you can use in an SQL statement.
$user_emailaddress = mysqli_real_escape_string ( $db, $_POST ['userEmail'] );
// Get password from client side and create a legal SQL string that you can use in an SQL statement.
$user_password = mysqli_real_escape_string ( $db, $_POST ['password'] );
// Get planType from client side and create a legal SQL string that you can use in an SQL statement.
$user_planType = mysqli_real_escape_string ( $db, $_POST ['planType'] );
// Create user.
// Note user Id is generated when a new record is inserted into a table.
$sql = "INSERT INTO admin (emailAddress,passcode,planType) VALUES ('$user_emailaddress','$user_Newpassword','$user_planType')";
$result = mysqli_query ( $db, $sql );
// if create user a successfully, jump to welcome page.
// otherwise print error information
if ($result ) {
echo "New record created successfully";
$_SESSION ['login_user'] = $user_emailaddress;
header ( "location: welcome.php" );
} else {
echo "Error: " . $sql . "<br>" . mysqli_error ( $db );
}
// Close Database
mysqli_close ( $db );
}
?>
the problem with your form is that the input tag with type="image" acts as a submit button when clicked. Check out this link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/image.
I guess that in your use case, you want these images to act as selection buttons for the available plan types. So I think you could replace then for an image tag with a radio button, or a select input with the three plans.

Displaying links/buttons when a user is logged on/off

I am truly a novice at coding and only succeed with trial and error. I use a WYSIWYG program to do all the main pages in my site and then add php coding to do some specified things.What I am trying to do right now is display a log in button along with a register and forgot password links to those forms, all of which I have built and working, have this display in the masterframe page when a user is not logged in and show another set of user name, profile, logout links when they are logged in. By themselves I have all these functions working, I just cant figure out how to do it this way. Any help or steering me in the right direction to teach me would be great. If you need to be paid for your help that can be arranged as well. Thank You.
update:
This is the code that I have right now and use, again I want to have the if else statement show one thing or the other on condition, and have it show in place of, all on the masterframes page.
// have this display if user is logged in
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
}
else
{
echo 'Not logged in';
}
?></span>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
//have this display if user is logged out
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
I have tried doing this but I keep getting a syntax error for unexpected '<'
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
}
else
{
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
}
?></span>
<?php
// Setting a session variable when customer is logged in.
$_SESSION['user_loggedin'] = 1;
$_SESSION['customer_id'] = $customer_id; // Some reference of logged in customer
$_SESSION['customer_name'] = $customer_name; // Customer information collected from DB or other resource.
// Deciding whether to display "Login" button or Logged in status / links
if ($_SESSION['user_loggedin']) {
echo 'Hi ' . $_SESSION['customer_name'];
echo 'My Account';
} else {
echo 'Login';
echo ' Register';
echo ' Forgot Password';
}
If you have some PHP function to check whether customer is logged in or not, you can use that function like this in lieu of if ($_SESSION['user_loggedin']) condition
if (UserLoggedin()) {
// Logged in links
} else {
// Links to be displayed when customer is logged out.
}
You are mixing both PHP and HTML code. Please correct.
You have to separate / embed HTML properly in your document while using php conditions.
Example:
<?php
$condition = true;
if ($condition) {
?>
<h1>This will be displayed when condition is true</h1>
<?php
} else {
?>
<h1>This will be displayed when condition is false</h1>
<?php
} // else ends
?>
Please try this:
<span id="LoginName1"><?php
if (isset($_SESSION['username']))
{
echo $_SESSION['username'];
?>
<div id="wb_Text2" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Profile</span>
</div>
<form name="logoutform" method="post" action="<?php echo basename(__FILE__); ?>" id="logoutform">
<input type="hidden" name="form_name" value="logoutform">
<a id="Logout2" href="javascript:document.logoutform.submit()">Logout</a>
</form>
<?php
}
else
{
?>
Log In
<div id="wb_Text3" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Register</span>
</div>
<div id="wb_Text1" style="">
<span style="color:#FFFFFF;font-family:Tahoma;font-size:12px;">Forgot Password?</span>
</div>
<?php
}
?></span>
Adding some code in your question would be nice, but if I understand your question correctly you might want to try this:
$logged_in = 0;
if($logged_in == 0) {
//all the stuff you want to show when the person is not logged in
$logged_in = 1;
}
if($logged_in == 1) {
//all the stuff you want to show when the person is logged in
}
In order to do what you are trying, you need to implement just a bit of logic on the code. The example of kerv is perfectly valid. The idea is that you will validate if the user is logged in or not, before rendering the html. For example:
if($userLoggedIn){
<div> Welcome to the site </div>
} else {
<div> Your are not logged in, please do so to continue </div>
}
I'll suggest you to edit the question with some code so we can properly help you.
Create a PHP session and use that session variable for your "IF" condition boolean.
i.e. if (session active)
{then display this object}
else
{dont display this object}
Here is some documentation on PHP 5 Sessions. PHP 5 Sessions
The neat thing about PHP is that it is completely interchangeable with HTML. Therefore you and assign elements to divs. Here is an example.
<html>
<body>
<?php
$loggedInTxt="";
$loggedOutTxt="";
if (*session*){
$loggedInTxt="<div>*some html content here*</div>"
}
else{
$loggedOutTxt="<div>*some html content here*</div>"
}
?>
<?php echo($loggedInTxt)?>
<?php echo($loggedOutTxt)?>
</body>
</html>
The idea is that you test the condition within the php and create php strings containing html elements. You can insert the anywhere in your html. So if you create a button and assign the code you used to create that button to a php variable then you can echo that variable(the code for the button) anywhere in your html script. I was blown away by the implications of php. I hope this helps simplify it! (This more of an overview. Do NOT copy and paste the code)

how to document.getElementById().innerHTML from different html/php file

Hello I am really new to coding in general but I got the basics of what I need.
I have my index.html that contains this:
<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="hidden">
</li>
<li class="page-scroll">
Portfolio
</li>
<li id="navbutone" class="page-scroll">
Login
</li>
<li id="navbuttwo" class="page-scroll">
Register
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
keep in mind I got this from a website template that I am editing so I didn't come up with this layout
and I have a php file that has some html in it to try and replace the contents of the list when this part of the code is run:
<?php
if($login_ok)
{
?>
<script type="text/javascript">
function logedin() {
document.getElementById("one").innerHTML = "Logout";
}
</script>
<script type="text/javascript">
logedin();
</script>
<?php
header("Location: index.html");
die("Redirecting to: private.php");
}
?>
This doesn't work and I have no idea if this is even close or not.
Thanks in advance for the help.
also I might add that they link to login.php where they login through an html form at the bottom of the php.
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" value="<?php echo $submitted_username; ?>" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
Register
<script src="index.html"></script>
</html>
Update: I found what I needed instead of messing with the php file i just put this into my index.html were the links will change out:
<?php
require("common.php");
if(empty($_SESSION['user']))
{
?>
<li class="page-scroll">
Login
</li>
<li class="page-scroll">
Register
</li>
<?php
}
else
{
?>
<li class="page-scroll">
Logout
</li>
<li class="page-scroll">
Members Page
</li>
<?php
}
?>
were common.php just connects to my database.
Look, I'm gonna give you some tips you could use for developing this:
First of all, you should use only PHP files (index.php instead of index.html), so it's easier to manage POST data and session variables.
So:
index.php
<?php
// This is PHP code, executed BEFORE any output is sent.
// First, to save data that works across page loads, we should use sessions, so we start a session that has to be called in every PHP page that uses that information.
// Variables use the format $_SESSION['variable_name'] = value
session_name('MySession'); // Give it a unique name
session_start(); // Start a session
?>
<html>
<head>
<title>Some title for your page...</title>
</head>
<body>
<!-- Here you will manage your template. It's plain HTML but, as this is a PHP file, you can include PHP code as well inside the PHP tags -->
<?php
// This is a PHP tag, here we can manage some PHP and output different HTML
// We check if the user logged in or not
if (
isset($_SESSION['logged_in']) // Always check if a variable exists before checking its value, or PHP will complain
&&
$_SESSION['logged_in'] == true
)
{
// The user logged in, show a LOGOUT link
echo '<a href=logout.php>Logout</a>';
}
else
{
// Otherwise, the user did not log in. Show a link to log in.
echo '<a href=login.php>Login</a>';
}
?>
<!-- Any other HTML you want, template or whatever -->
</body>
<html>
Now, we used two files: login.php and logout.php. The first one will show a form, the second one will logout and redirect to the index page.
login.php
<html>
<head>
<title>Please log in</title>
</head>
<body>
<form action="do_login.php" method="post"><!-- Notice another file: do_login.php -->
<input type="text" name="username" placeholder="Your username" />
<br />
<input type="password" name="password" placeholder="Your password" />
<br />
<br />
<input type="submit" name="submit" value="Log in" />
</form>
<body>
</html>
Now we need the file that processes the login (do_login.php in the form) and stores session data.
do_login.php
<?php
// We use the same session as before
session_name('MySession'); // Same name as index.php and all other files
session_start();
// This will be a pure PHP file that stores session data and returns to the index page.
// You want to check data against databases here, but we will use static information for easier reading.
// You also want to check data to be correct, but we won't do that here for simplicity.
$username = $_POST['username']; // This is the "username" from the form.
$password = $_POST['password']; // This is the "password" from the form.
if (
$username == 'John' // Username is John
&&
$password == 'MyPassword' // Password is MyPassword
)
{
// Here the login data is correct, let's save some session variable that says the user correctly logged in.
// Note that this is potentially extremely INSECURE! You should save other data and check every request, but this is just for you to start learning.
$_SESSION['logged_in'] = true;
// Ok, user logged in. Redirect to the index.
header('Location: index.php'); // Send a redirect header (note that NOTHING has been echoed before in this page).
exit;
}
else
{
// Login data incorrect. Redirect to an error page, let's say login_error.php
header('Location: login_error.php');
exit;
}
?>
Now the file to log out:
logout.php
<?php
// First we recreate the session and destroy the variable(s) that say the user has logged in.
session_name('MySession'); // Same name as before
session_start(); // We start the session. At this point, all session variables have been recreated.
unset( $_SESSION['logged_in'] ); // We destroy the variable
session_destroy(); // Now we drop the session
header('Location: index.php'); // Redirect to index.php
exit;
?>
Now we only need the login failed page:
login_error.php
<html>
<head>
<title>Login error!<title>
</head>
<body>
<h1>Login error!</h1>
<p>The login data was incorrect. Try again.</p>
<br />
<p>Go back to the index page</p>
</body>
</html>
I hope this helps, but you really need to read some tutorials.
Have fun!
Use used " instead you should have used '
<script type="text/javascript">
function logedin() {
document.getElementById("one").innerHTML = "<a href='logout.php'>Logout</a>";
}
</script>
Change to following line:
document.getElementById("one").innerHTML = "Logout";
" characters has to be escaped.
And link has to be like that
link
It's worth to use i.e. firefox plugins like JS console where you could see in what line error occurs.
I've got it figured out. in the index.html i just put
<li class="hidden">
</li>
<li class="page-scroll">
Portfolio
</li>
<?php
require("common.php");
if(empty($_SESSION['user']))
{
?>
<li class="page-scroll">
Login
</li>
<li class="page-scroll">
Register
</li>
<?php
}
else
{
?>
<li class="page-scroll">
Logout
</li>
<li class="page-scroll">
Members Page
</li>
<?php
}
?>
does exactly what I need and no messing with the other php files.

Categories

Resources