Flag icon beside logged in user - php

How to add Flag icons next to logged in username in navbar?
Using Php oop MySQL database on a signup/login system. I have users_country as one of my database columns. This is put in the table as the classic two letter format, example: Netherlands is stored as NL.
After a user logs in I have my nav bar change from sign up, sign in
to usercountry & username, logout
I do that with php
Session start ()
echo $_SESSION[“usercountry”]
echo $_SESSION[“useruid”] (their username)
Now my navbar reads, example:
NLtestuser, logout
How do I go about replacing just the NL country code next to the username in the navbar to be an icon with the corresponding flag.
I would like the user to log in and the flag they selected upon signup will show instead of the flags two digit id.
I already have a flagicons folder in img folder in the root folder that contains all the flag icons with their corresponding countryid.png, example: nl.png
What is the simplest way to just change the country code to the icon flag when the user is logged in?
Thanks.

This is your code reformatted, with the code to show the flag:
<ul class="left-side">
<?php
if(isset($_SESSION["userid"])) {
?>
<li><img src="/img/<?php echo strtolower($_SESSION["usercountry"]); ?>.png" alt="flag"></li>
<li>logout</li>
<?php
} else {
?>
<li><a href='Signin.php'>Sign in</a></li>
<li><a href='signup.php'>Sign up</a></li>
<?php
}
?>
</ul>
Have a look at what the HTML source looks like if the image doesn't show up.

Related

Set navbar selection to class active while also having the Login link switched to Logout if signed in?

I'm trying to figure a way to have "Login" be set to logout, when a user is signed in, but also having their current navbar selection be highlighted in blue?
I moved the html in the php below out of the html body. This html was repeated in every file for each page and <a class="active" href="index.php">Home</a> was different according to the page selection. I tried to minimize my code repetition by moving it to an external file to be called in via <?php include ("scripts/navbar.php"); ?>.
For example Here you can see Contact is blue because it's the page I'm currently on. But if a user is signed in, I want Contact to still be blue if they're currently on the Contact.php page, but to have Login changed to Log Out for the session. I have my logout script functioning correctly, but I'm not sure how to dynamically change both login to logout, while setting the selection to blue.
<?php if ( ! isset($_SESSION['username'] ) ): ?>
<div class="topnav">
<img src="assets/images/header5.jpg">
Login
Contact
About
Sample
Fees
Vintage
Services
<a class="active" href="index.php">Home</a>
</div>
<?php else: ?>
<div class="topnav">
<img src="assets/images/header5.jpg">
Logout
Contact
About
Sample
Fees
Vintage
Services
<a class="active" href="index.php">Home</a>
</div>
<?php endif; ?>
I ideally didn't want to have 6-7 different files for the navbar,
<div class="topnav">
<img src="assets/images/header5.jpg">
<?php if (!isset($_SESSION['username'])) : ?>
Login
<?php else : ?>
Logout
<?php endif; ?>
Contact
About
Sample
Fees
Vintage
Services
<a class="active" href="index.php">Home</a>
</div>

How to make an HTML element hidden and viewable only for certain role only in wordpress?

Newbie wordpress/php student. I want to make a certain element viewable only for a certain user-role in wordpress and hidden for all normal subscribers/non-subscribers.
Its for a property page on a real estate website. I want to make a specific tab element hidden so only for certain users(agent user role) to be able to see.(The element will contain information such as property owners phone number. I will take the 'floorplan' tab on the property page and change it to 'owner info' tab.
It's tough to answer precisely without seeing the PHP template code for these tabs, but in general, this is as simple as putting that tab's HTML inside a PHP conditional.
// your code that checks if this home is owned by the current user
// var $owner_viewing_property = is_owner_viewing();
<ul class="tabs">
<li class="tab">Features</li>
<? if ($owner_viewing_property) { ?>
<li class="tab">Owner Info</li>
<? } else { ?>
<li class="tab">Floor Plans</li>
<? } ?>
</ul>

Recover url through ID

every day, unloading a file that contains a set of url I have to change every day in css menu that I have the page. The only thing that does not change the name assigned to the url. You assigning each a name or other ID, go to the new url css menu without changing it every day?
Today
file.php
<a href='http://playlist.m3u.com/playlist1.m3u'>playlist1</a><br/>
<a href='http://playlist.m3u.com/playlist2.m3u'>playlist2</a><br/>
<a href='http://playlist.m3u.com/playlist3.m3u'>playlist3</a><br/>
Tomorrow
file.php
<a href='http://playlist.m3u.com/playlist4.m3u'>playlist1</a><br/>
<a href='http://playlist.m3u.com/playlist5.m3u'>playlist2</a><br/>
<a href='http://playlist.m3u.com/playlist6.m3u'>playlist3</a><br/>
Every day changes the url but not the playlist name
menu_index.html
<ul>
<li><a href='http://playlist.m3u.com/playlist1.m3u'>playlist1</a><br/></li>
<li><a href='http://playlist.m3u.com/playlist2.m3u'>playlist2</a></li>
<li><a href='http://playlist.m3u.com/playlist3.m3u'>playlist3</a></a></li>
</ul>
On this file, I change the link every day, but I would find a way to not edit it every day if possible.
The index is a menu in css menu dropdown, I would like to avoid having the classic white page with hyperlinks.
if i'm right understand you:
<?php $array = array('playlist1','playlist2','playlist3');?>
<ul><?php
foreach($array as $val){
?><li><a href='http://playlist.m3u.com/<?=$val?>.m3u'>playlist1</a><br/></li><?php
}
?></ul>

My session variables are lost when I redirect to main page with http

I am experiencing a very strange problem.
I have 3 pages :
home.php
forums.php
projects.php
Every page has a menu like this:
<ul>
<li><a href='http://www.mysite.com'>Homepage</a></li>
<li><a href='forums.php'></a>Forums</li>
<li><a href='projects.php'></a>Projects</li>
</ul>
When the user is logged in sessions are set up and in every page on top username is shown.
If I'm in Projects page and my username is on top as I am logged in and now I want to go to main page, when I cycle through projects and forums the username is on top and sessions are not lost. When I click Homepage it goes to main page sessions are lost and the username is lost on the top.
I think the problem is with the first link Homepage.
When I do it index.php everything works fine but with this http:// it is not working. Actually sessions are still there but not showing up.
UPDATE
I think I found the problem
I converted all menu links to full links with http not only php files like this
<ul>
<li><a href='http://www.mysite.com'>Homepage</a></li>
<li><a href='http://www.mysite.com/forums.php'></a>Forums</li>
<li><a href='http://www.mysite.com/projects.php'></a>Projects</li>
</ul>
And everything works fine.
I am curious and I want to expand my knowledge. If anyone knows why is this happening please let me know.
converted all menu links to full links
No
http://www.mysite.com is not a valid URL, some webservers will assume that the path is '/', some browser will assume the path is '/' but most browsers will send the URL as is, and the webserver will usually send a redirect back to the browser.
As long as the host is the same in all cases or omitted, then the code will behave as you expect - but you really should use proper URLs to avoid subtle semantic issues i.e.
<ul>
<li><a href='http://www.mysite.com/'>Homepage</a></li>
<li><a href='http://www.mysite.com/forums.php'></a>Forums</li>
<li><a href='http://www.mysite.com/projects.php'></a>Projects</li>
</ul>
or...
<ul>
<li><a href='/'>Homepage</a></li>
<li><a href='/forums.php'></a>Forums</li>
<li><a href='/projects.php'></a>Projects</li>
</ul>

PHP - DYNAMIC PAGE counter increment

I have a dynamic page , which fetch data from database . Say for eg there are 2 record which is generated depending on query . So what i want to do is when some one click the Home link on 1st record , it should go to next page and over there php code is there which will increment the counter that is how many times the page is visted
Similarly when 2nd record Home link is clicked it should go to some page and then again again counter for that page is incremented
so say eg
Home Page - 1st Record - Page : http://www.abc.com/Wakad-T4248349
Home Page - 2nd Record - Page : http://www.abc.com/Himn-T3333333
Or another way of doing this , please help
<div id="menubar">
<ul>
<li class="current_page_item">
Home
<?php $_SESSION['Home']=$row['ID']; echo $_SESSION['Home'];?>
</li>
</ul>
</div>
<div id="menubar">
<ul>
<li class="current_page_item">
Home
<?php $_SESSION['Home']=$row['ID']; echo $_SESSION['Home'];?>
</li>
</ul>
</div>
Why not just have a field in the database which keeps track of how many times that particular page has been fetched? Or are you looking for unique IPs?
Assuming you don't control abc.com, you'll have to make the link go to one of your own pages:
<a href="redirect.php?link=abc.com/Wakad-T4248349"> ...
and on redirect.php, increase your counter (which you'll probably want to store in a DB...) and redirect them to the page.

Categories