User Name display as after login - javascript

I want to display logging user name. But below given code working perfect but the same page only working. I want home.php to display.
Login.php
if($check_user>0)
{
$_SESSION['user_name']=$username;
echo "<script>window.open('home.php','_self')</script>";
}
Home page coding
<?php echo($_SESSION['username']); ?>
But this code undefined index error showing.
How can call session username in home page

You have undefined index because you setting $_SESSION['user_name'] and try to read $_SESSION['username']. Change your home page code to: <?php echo($_SESSION['user_name']); ?>

Related

Change navigation menu when user is logged in HTML

I have a site in HTML, where I implemented login and register system using PHP.
When there is no user logged in, the navigation item "Contul meu" from every page need to send me to "gotosigning.html" page.
However, when the user is logged in I want that the menu item "Contul meu" from every page to send me to "account.html" page.
Some menu items for example, in the index.html page:
<li class="active">Acasă</li>
<li>login</li>
<li>sign in</li>
<li>Contul meu</li>
the gotosigning.html page redirects to a page where you can select if you want to sign in or login, and based on the selection here you go to login.html or signin.html.
This is the basic example,when the user is not logged in, but when it is, the last item should become <li>Contul meu</li> in all the pages.
login.php, which is implemented in the login.html and signup.html pages
<?php
$link = new mysqli("localhost", "root", "", "graphicdesign");
if($link->connect_error){
die("ERROR: Nu s-a putut realiza conexiunea la baza de date " .$link->connect_error);
}
session_start();
$email =$_POST['email'];
$password=$_POST['pass'];
$result = $link->query("SELECT email_cl, parola_cl FROM clienti WHERE email_cl= '".$email."' AND parola_cl= '".$password."'");
if($result->num_rows == 0 ) {
echo "Datele nu corespund!";
}
else {
$_SESSION['logged in']=true;
$_SESSION['email']=$email;
echo "Login cu succes!";
echo "<script>setTimeout(\"location.href = '../account.html';\",1500);</script>";
}
?>
How can I make the server know all the time which the user is logged in?
And how can I tell the HTML pages when to update their navigation based on that state of logged in or not?
The only thing I thought about was to duplicate all the pages ( but they are too many ) and then implement some code to test if the user is logged in to the server can choose the choice with the right menu... but doesn't seem okay at all...
Thank you!
First, you need to rename all your pages from .html to .php. There is absolutely no difference between the two extension except for this: if the page ends in .php, then the PHP processor knows to interpret any PHP code found between <?php and ?> tags. If the page ends in .html then the PHP will not be processed. HTML will continue to work exactly the same. Try it now - create a test page with some HTML in it and name it with the .php extension. You will see it works exactly like one ending in .html
In order to change the navigation items after the user has logged in, you can refer to the $_SESSION variables that you set when they logged in. Note: do not use spaces in session variable names - underscore chars (eg logged_in) are fine, though.
Example:
<?php
if ($_SESSION['logged_in'] == true){
$out = '<li>Contul meu</li>';
}else{
$out = '<li>Contul meu</li>';
}
echo $out;
?>
And, most important - make sure that you put session_start(); at the top of every PHP file.
As a side note, I personally like to have a <?php ?> section at the top of my PHP files that contains as much of my PHP code as possible. For example, I would place the above code right up at the top of the file -- before any HTML -- before <!DOCTYPE html>. Then, I have all my HTML code and -- where it belongs -- I echo out the PHP variable, like this:
<li class="active">Acasă</li>
<li>login</li>
<?php echo $out ?>
<li>sign in</li>
(I intentionally placed your Contul meu menu item for DEMO purposes, so that you can see stuff above it and also below it. In your example, it was the last menu item which would not be as clear for demo purposes.)
you can display your menu based on condition. Put a check using session on the top of every login/logout and signin page like that:-
Note: you make your start session on top of every file.
<?php
session_start();
if(isset($_SESSION['email']) && !empty($_SESSION['email'])){
?>
Logout
<?php
}else{
?>
<li class="active">Acasă</li>
<li>login</li>
<li>sign in</li>
<li>Contul meu</li>
<?php } ?>

Error Message Display - Form Data PHP Entry in Wordpress

Currently, we have a PHP Form that uses an entry for a redirect to Private sites within our main website, of which code we use the following:
<?php
//Turn the content from text box into variable
$page=$_POST['text'];
if($_POST['text']=='sit') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/index.php/private/sit\">";
}
else if($_POST['text']=='pony') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/wp-content/uploads/sites/2/client/pony/index.html\">";
}
Originally, we had implemented a final line that if someone inserted something wrong, it would refresh the page:
else {
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com\">";
}
?>
How to show a message that says "Error" in Bold and Red next to the form?
You can send a parameter in the URL you use to refresh. And then check on the parameter after the URL refresh by Javascript and add the error.
In this case you will echo something like this instead
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com?error=1\">";
Then in your html, use Javascript or PHP to check on the URL param and render error with style.

External php file that is loaded into index.php with .load() method not recognising $_SESSION variables

So my main page loads other php pages into it with click of a button so it can be a single page website without having to load all the content at once.
index.php
<?php
session_start();
?>
<head>
$('#btnPetShop').one( "click", function(){
$( "#page_shop" ).load( "shop.php" );
});
</head>
<body>
<?php
echo session_status();/----Always returns 1, no matter if logged in or not----/
if(isset($_SESSION['admin']))
{
if($_SESSION['admin']==1)
{
/----this part works, I am logged in as admin----/
}
}
?>
<div id="page_shop"></div>
</body>
shop.php
<?php
if(isset($_SESSION['admin']))
{
if($_SESSION['admin']==1)
{
}
else{}
}
else{} <----I end up here as if $_SESSION['admin'] is not set----/
/----code entered here loads fine----/
?>
The idea is to make a delete and edit button (if you are logged in as admin) on every article in shop.php.
Problem is that $_SESSION['admin'] is recognized on index.php, but not inside shop.php
I tried typing the content of shop.php directly into and it works, the problem is that i want it to load with a click of a button.
Where ever ( in any PHP page) if you want to use any Session variable then you have to first declare session_start(); so, in your case if you have $_SESSION['admin']="User1234" on index.php and if you want to use value of $_SESSION['admin'] in shop.php then you have to again declare session_satart(); and then use it. For example, If let consider that index.php has session variable $_SESSION['admin']="User1234" and now you want to print "Welcome User1234" on shop.php then you can do it as shown below.
index.php
<?php
session_start();
$_SESSION['admin'] = "User1234";
?>
Shop.php
<?php
session_start();
echo "Welcome, ". $_SESSION['admin']
?>
Output:
Welcome, User1234
You need to add session_start(); in each page that needs access to the session data.
See: http://php.net/manual/en/function.session-start.php
EDIT
Since the solution mentioned in my original answer does not work for you and session_status() returns 1 in your code, it means sessions are enabled on your server. There is only one thing left which could explain that your sessions are lost:
You are loading shop.php with an AJAX request, is the URL exactly in the same domain as index.php? Try to add the full path before shop.php to see if this solves the issue.
Just to be clear, if your index.php runs on http://localhost/test/index.php, your new code will be:
$( "#page_shop" ).load( "http://localhost/test/shop.php" );
Well okay, I feel dumb... I found the solution.
I had this as a script
$('#btn_logout').click(function(){
<?php session_destroy();?>
});
This is a big no no and if you do this you should be ashamed

how to stop direct access a page without login in php

I have login page, and after login I have user information related pages. so i don't want to direct access those pages from url. Instead of it will redirect to login page. how can I do that.
my sessions.php page code
<?php
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
?>
in your login page: if user successfully logged-in then set any session variable i.e. $_SESSION['userLogin'] = "Loggedin";
Then
each and every page check user loggedin or not by below code:
session_start();
if(empty($_SESSION['userLogin']) || $_SESSION['userLogin'] == ''){
header("Location: http://example.com/login.php");
die();
}

Avoid Logged in users to access index.php

I have a ticketing website and I want to avoid users from opening index.php after they logged in. When they are logged in, they are automatically redirected to dashboard.php. Because my Login page is my index.php file and I want to customize it for login only. I want to write some code like below in PHP or jQuery or JavaScript:
<?php
$call_user = $site_calls->call_user;
if ($call_user <> 0){
//redirect to ("dashboard.php");
}
?>
If someone already logged in , they are redirected to dashboard.php whenever they want to access to index.php page.
`Simplest code, working shown below. Set a session variable after login, check for that session variable in index.php, if its set, redirect to dashboard.php`
<?php
session_start();
if(isset($_SESSION["user"])) //change to your session variable
{
header("Location: dashboard.php");
}
?>

Categories

Resources