Test if a link with specific text has been clicked (php) - php

EDIT: is there a way to do this without reloading the page? I have a class that shows up when "Login" is pressed but it goes away quickly because the page is reloaded
I'm trying to test to see if a specific link text has been clicked because the same link can either have the texts "Login" or "Logout", but it's not registering that it's a valid link at all
index.php:
<body>
<nav>
<ul>
<li id="login">
<?php
if (isset($_SESSION['logged_user']))
echo "<a href='index.php?link=login'>Login";
else
echo "<a href='index.php?link=logout'>Logout";
?>
</a>
</li>
</ul>
</nav>
//LOGOUT
if ($_GET['link']=='logout') {
unset($_SESSION["logged_user"] );
unset( $_SESSION );
$_SESSION = array();
session_destroy();
}
error: Undefined index: link

Change this to
if ($_GET['link']=='logout') {
this
if (isset($_GET['link']) && $_GET['link']=='logout') {

When PHP page gets load, the PHP compiler checks for link property in GET array which is not there until you clicked on it. So you have to take care this condition with isset function so that it only validates when its available.
if (isset($_GET['link']) && $_GET['link']=='logout')
Hope this helps!!

I guess session_start(); is probably missing at the top to allow you to use $_SESSION['logged_user'];
if(session_status() == PHP_SESSION_NONE){
session_start();
}
And also :
<?php
if (isset($_GET['link']) && $_GET['link']=='logout') {
unset($_SESSION["logged_user"] );
unset( $_SESSION );
$_SESSION = array();
session_destroy();
// echo "Hello";
}
?>
Good luck

Related

Display a link only if user is an admin

I am very new to PHP and I am trying to make a registration form only for an admin(no need for other users). I want to show one of the menu nav ("Add photo") only to admins.
LOGIN.php:
<?php
include_once 'header.php';
$username = "Efren";
$password = "111";
if (($_POST['txt_uname_email'] == $username)&& ($_POST['txt_password'] == $password)) {
session_start();
$_SESSION['admin_is_logged'] = true;
echo '<script type="text/javascript"> window.open("homepage.php","_self");</script>';
}
This is the part of the header that I am trying to show only to admins:
<?php
if (isset($_SESSION['admin_is_logged']) && $_SESSION['admin_is_logged'] == true){
echo '<li>add photo</li>';
}
?>
</ul>
Right now “add photo” is hidden both to admin and other visitors.
You need to start session on every page you want access to $_SESSION variable. I saw your session_start is inside if statement. Just set it on top of every file (where you need session) and it should work.
Put
session_start();
on file beginning just after <?php

Trouble returning session data (user name)

I'm trying to integrate a php login script that I have working, but I can't seem to get simple php calls going on a page. On this user profile page, I want to simply have the user name displayed (mysql field is "name"). The user is logged in and the session carries through, but on this page, all I see is the text "Here is your profile info..." What might be wrong in the code to prevent the user name from displaying?
<?php
include_once('classes/check.class.php');
include_once('header.php');
if( protectThis("*") ):
if(!isset($_SESSION)) {
session_start();
}
if(isset($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
?>
<br />
Here are is your profile info...
<?php
else :
?>
<div class="alert alert-warning">
<?php _e('Only signed in users can view what\'s hidden here!'); ?></div>
<?php
endif;
include_once('footer.php');
?>
For check session is set already use session_id() Also check you have set $_SESSION['jigowatt']['name'] already with empty()
if(session_id() == '') {
session_start();
}
if(!empty($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
else {
echo 'username is empty';
}
You need to put session_start(); at the very top of the page. No white space can be put before that. Try if that works.
First you need to write the sessions at the very top of the page if it works than okay else you can try this.
Just append this 2 function before and after the session_start();
Like this
ob_start();
session_start();
ob_end_clean();

Changing Index Page According to Login

Im creating an website where i am checking for login and redirecting the user to the index page, if his login was successful i want him to see something else instead of the login button
i have followed this approach for my query
<?php
if(!isset($_SESSION['uid']))
{
?>
<span class="Login">Login</span>
<?php
}
else if(isset($_SESSION['uid']))
{
?>
<span>Post</span>
<?php
}
?>
it doesn't seem to work quite the way i want. The 'Login' span is always visible, it would seem that the $_SESSION['uid'] is not being set, but that is not the case. To be honest i don't even know if this is the correct way of doing this
You need to put session_start(); in each page that need to access the session data before accessing (or creating) any session data.
See: Session Manuel
<?php
session_start();
$linkPage = 'login.php';
$linkName = 'Login';
if(isset($_SESSION['uid'])) {
$linkPage = 'postThread.php';
$linkName = 'Post';
}
?>
<span class="link"><?php echo $linkName; ?></span>

Changing an output for a link in php

I'm trying to get a link in my menu to change once the user logged in. Meaning that "My Page" is displayed, rather than "Log In". Now, the user can log in and log out. Yet the name/link will not change directly after the user logged in.
My code for the menu: (I use include('menu.php' on all sites. For flexibility of the code)
<div id="menu">
<ul>
<li>Home</li>
<li><?php echo $mypage?></li>
<li>Community</li>
<li>Media</li>
<li>Contact</li>
</ul>
My code for the config.php:
<?php
session_start();
$mypage;
if(isset($_SESSION['name'])) {
$mypage = 'My Page';
}
else {
$mypage = 'Log In';
}
?>
Only when the user clicks on another menu the $mypage variable will update. I've tried refreshing the page with a header() function. But this only lead into a redirection-loop.
It would be great if someone could help me out.
EDIT
To clear some things up:
For a better understanding of the website:
http://www.askmephilosophy.camilstaps.nl/
(The only thing that can be viewed (so far) are the 'Welcome' and 'Log In'. After a login you will be redirected to a mypage.php site. (this can also be accessed by just typing it after the name. */mypage.php)
So if I understand:
1/ you log-in
2/ you are logged but the name stay "Log In"
3/ you refresh the page then the menu changes to "My Page"
I think that you set $_SESSION['name'] AFTER you execute this
if(isset($_SESSION['name'])) {
$mypage = 'My Page';
}
else {
$mypage = 'Log In';
}
Try something like this:
if (isset ($_POST['name']) && isset ($_POST['password']) && ($_POST['password'] == $hash)) {
$_SESSION['name'] = $_POST['name'];
}
if(isset($_SESSION['name'])) {
$mypage = 'My Page';
} else {
$mypage = 'Log In';
}

$_SESSION Help How To Use It In One Page? (With Redirect?)

I wonder if the following is possible?
My website have a secret link (website.com/?secret=yes) I wanted to make the url look (website.com) after they have entered + show them the special content because they are from the secret link.
I thought about something like this possibly can work?
1. User Navigate to (website.com/?secret=yes) create a $_SESSION and make it true + Instant navigate to website.com
2. Checks to see if $_SESSION = true if true show the special content?
I have the the following code:
<?php $secret = isset( $_GET[ 'secret' ] ) ? sanitize_text_field( $_GET[ 'secret' ] ) : false; ?>
<?php if( 'yes' === $secret ) : ?>
<div>
<p>My secret content</p>
</div>
<?php endif; ?>
If it was possible to make use of this + $_SESSION or if you have any ideas? I don't really know how $_SESSION works but i read in php.net about it and i think it's possible?
Thank you!
p.s I use wordpress.
You could try this inside of your index.php page:
<?php
session_start();
if(isset($_GET['secret']) && $_GET['secret'] === 'yes') {
$_SESSION['secret'] = true;
header('Location: www.website.com');
}
if(isset($_SESSION['secret']) && $_SESSION['secret'] === true) {
//Yay! Display secret content
}
?>
I've rewritten your code to work without a redirect at all. The secret sections can exist in the same page as the landing page, or in different pages. I've also modified the logic so that it will not "forget" the secret status if they come back to the landing page without "?secret=yes" in the URL. I've also updated the code with some basic Javascript that will allow you to remove the "?secret=yes" from the URL without redirecting.
This code would go in any landing page:
<?php
session_start();
if(isset($_GET['secret']) && 'yes' === $_GET[ 'secret' ])
{
$_SESSION['secret'] = true;
}
?>
This code would go in the head section of your page, or the body section if you can't access the head.
<?php
if(isset($_GET['secret']) && 'yes' === $_GET[ 'secret' ])
{
echo '<script>history.pushState({},"","http://yourdomain.com/pageinurl/");</script>';
}
?>
This code would go on any page with secret code:
<?php if(isset($_SESSION['secret']) && true === $_SESSION['secret']) { ?>
<div>
<p>My secret content</p>
</div>
<?php } ?>

Categories