Changing an output for a link in php - 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';
}

Related

Test if a link with specific text has been clicked (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

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>

how do i get people roam my site when they are not logged in?

Today my question is how do i get people to roam the site with out logging in, I have tryed loads and loads of diffrent ways, when i tried to roam my site when i was not logged in it just used to redirect me to my login page, but when i tried my most recent code (the one below this post) it just comes up with this error, Undefined index: username in E:\wamp\www\login\main.php on line 6
<?php
ob_start();
//session
session_start();
$_session_username = $_SESSION['username'];
if (!isset($_session_username))
{
echo"Hello i'm sorry to say this but your not logged in <a href='login.php'>Log-in</a>";
exit();
}
else
{
echo "hello, ".$_session_username." <a href='logout.php'>Log out</a>";
}
ob_end_flush();
?>
Put simply you were on the right path, however you can't assign $_SESSION['username'] to a variable and then check if it is set. You first need to check if the $_SESSION['username'] is set, and then if it is you are able to assign it to a variable.
<?php ob_start();
//session
session_start();
if (!isset($_SESSION['username']))
{
echo"Hello i'm sorry to say this but your not logged in <a href='login.php'>Log-in</a>";
exit();
}
else
{
$_session_username = $_SESSION['username'];
echo "hello, ".$_session_username." <a href='logout.php'>Log out</a>";
}
ob_end_flush();
?>

Making a Function-Activated Link Appear Without Having to Refresh Browser

I'm trying to use the code below to make the <a href='http://www...com/.../footervote.php'>Vote</a> link appear if a user logs in and a user shows up in the function getEditorsList(). The vote link only appears if the browser is refreshed.
Any idea how I could make the vote link appear without having to refresh the browser?
Thanks in advance,
John
index.php:
<?php
require_once "header.php";
//content
include "login.php";
// more content
require_once "footer.php";
?>
In header.php:
<?php
error_reporting(0);
session_start();
require_once ('db_connect.inc.php');
require_once ("function.inc.php");
$seed="0dAfghRqSTgx";
$domain = "...com";
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
?>
login.php:
<?php
if (!isLoggedIn())
{
if (isset($_POST['cmdlogin']))
{
if (checkLogin($_POST['username'], $_POST['password']))
{
show_userbox();
} else
{
echo "Incorrect Login information !";
show_loginform();
}
} else
{
show_loginform();
}
} else
{
show_userbox();
}
?>
Do you set $_SESSION['loginid'] after your in_array query? If you render header.php first, in_array returns false (although the session has been started, but loginid will be set a few lines down the road in login.php).
Move this:
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
from header.php to login.php like this:
else {
show_userbox();
if (in_array...
}
If the link is present but hidden you use some DHTML (JQuery / Scriptaculous) to set the display/visibility attributes correctly.
If the link is not present in the original html (preferable for security reasons) then when the login occures fire off an AJAX request that returns javascript that will insert the link in the correct location (parent element).

Categories