set display by role programmatically in Drupal 7 - php

I want to display this text link, <p>Click here to register.</p> in the body of a larger block of text on a page and but I want the link to show up when some is logged in. I want to use php conditional to show this other link when someone is instead anonymous on the site,
<p>You need to login before you can register. Please Click here to login.
I want a code like:
<?php
if user=="logged-in";
echo "<a href ='register'>Click here to register.</a>";
else echo "<a href ='login'>Click here to login.</a>"
?>
I know I must not have written perfect php but I do not have problems with the php, what I need is the Drupal syntax for if user == "logged-in" and if user == "not-logged-in".
I do not want to use a block and start setting visibility by role. I want to use code as described.
Thanks.

See the docs
You want something like this. Both of your link descriptions need some work though. What you'd most likely want to do is display both the register and the login link to anonymous users.
<?php
if (user_is_logged_in()){
//user is logged in
}
else{
//user is not logged in
}
?>

Related

How to Restrict Access to a Page with PHP if statement

I have a PHP project with user account status in the database. It contains status such as active, inactive, suspended etc. I need an if statement in the index page that can Restrict access to a particular page or link if the account is set to inactive.
Your response is great appreciated. Thank you in advance
If you wanted to prevent links to certain pages from showing to users that didn't have a status of active, you might do something like this:
<!-- Non active state links -->
About us
Contact us
<!-- Active state only link -->
<?php
if($user->status === 'active'){
echo 'My Account';
}
?>
Then on each of the restricted pages themselves, you might do something like this:
<?php
if($user->status !== 'active'){
echo "Forbidden!";
die();
}
*** The rest of your code here ***
?>
To prevent access to a particular page, you need the logic to be present on that page, you can't control who can access what solely from the index page.
I suppose you are using Native PHP:
At the file which contains the code you want to restrict:
<?php
if(!$myUser->isActive){ //User is not active
header(File you want to redirect in case is not active.php)
}

Php shadow login into forums and return data to iframe

I have a page whit login forums
Forum1
Forum2
Forum3
</br>
<iframe width="100%" height="100%" name="myiframe"></iframe>
<p>LogOut</p>
I have registration credentials in each of them, I have 5 people who want read this forums with my credentials, I do not want to show them credentials but I want to let whem read thoose forums with my credentials. So in my page on href pressed in iframe must be loaded content of each forum already with my credentials (which must be not shown to user) (so i dont warry what they can change any info or something else in my profiles). Also i need to track what whey do on forums to control who what can do ( such thing which link is pressed).
It is possible to do with PHP? Please show me i right direction to solve this problem.
I guess it's way easier to let those users register a own account.
In case you still want to use a PHP code to let those users view the forum without knowing your credentials, you could create a PHP class like this:
<?php
class Forum
{
var $url = '';
public function getContent()
{
$content = #file_get_contents($this->url);
return $content;
}
}
?>
and create objects for each forum u want to show.
<?php
require_once('Forum.php');
$getForum = new Forum();
$getForum->url = 'http://www.google.de';
echo $getForum->getContent();
?>
That's just a example though, you would have to add your credentials and might use http://php.net/manual/en/book.curl.php to sign in to the forum.

Trying to add html w/ link using echo after else in php

I have content only available to registered users - guests see an intro but if they click to read more are shown a message to login or register - I need to add a link to the registration page in the message they see.
Here is what I have tried:
if (!$user->guest) {
echo $this->item->fulltext;
}
else
echo '<h3><b>Please login or <a herf="/login/register">register</a> to view the entire article</b></h3>';
?>
My text shows correctly & the word register turns blue, but it is not linking.
Please advise. Thank you in advance.
When you can tell me what a herf is, I'll wonder myself.
It's spelled href not herf.
=>
register

Display Admin Link On Every Page

My question is about how do I place a link on the side of every page that leads to the admin page once someone has logged on to my application successfully? I have a sample site I just built, and I would like a link to the admin page available in the navigation column to the right of the page which is displayed site-wide. But if the person is not logged in, they don't see the link, but will continue to see the usual links.
My background is totally different from web development, so forgive my stupid question.
I'm using PHP and MySQL for the application.
Without seeing how you display your menu or what key is used for the session, Ill assume some things:
<?php
session_start();
// do your login stuff and set session as logged in
$_SESSION['logged_in'] = true;
?>
Then in your menu or how ever you display it:
<?php
//navigation column
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true){echo 'Admin';}
//navigation column continue with rest of links
?>
Or the ternary operator assign link to a variable
<?php
$adminLink = (isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true)?'Admin':'';
echo $adminLink;
?>
You should use a session variable to track the user's session and see if they are logged in.
if(isset($_SESSION['id'])) echo 'Admin Area';

check if user activated their account via email and display message after clicking on activation link PHP/JavaScript

I'm working with the Zend Framework on a project that requires the user to activate their account by checking their email and clicking on the activation link. The activation link works fine and the user can login once their account is set active. My question is how can i display a message when the user clicks on the activation link and get redirected to the home page, acknowledging that their account has been activated.
I was trying to write an if statement to check if HTTP_REFERER = the URL of the activation link. But that didn't work. Any help would be greatly appreciated.
Here's the if statement:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
<?php if ($_SERVER['HTTP_REFERER'] == 'activationLinkGoesHere'){ ?>
$("#suc_message").html('<?php echo 'Your account has been activated!';?>');
$("#suc_message").fadeIn(2000);
$("#suc_message").fadeOut(20000);
<?php } ?>
});
</script>
Thanks :)
Is there a reason you couldn't set a session variable on the activation page and then check for it in php on the home page? If it's set, use php to inject some javascript (just a simple boolean flag should do) that you then check when the page loads.
If you use Zend Framework, then you must use FlashMessenger. It's designed for that exact purpose.
Remember to use the helper's static method in your controller's action.
Then for the view use noumenal's excellent view helper; you can then put it in your layout or view script.
It's that easy. Hope it helps.
Sorry for the original answer, I did not read this properly.
I personally would just add something on the end of the url:
http://mysite.com/thanks.php?Reg=1
..and look for the reg part and inject the message if it exists.
edit
Returning to this, I'll add that it is very important to keep the url addition to something that is a boolean value. For example, you could display multiple messages if you found ?Reg=1 or ?LoggedIn=1 and displayed "You registered OK" and "You logged in OK" respectively.
Don't pass anything in that your javascript then uses for display or otherwise.
Don't make it fire any actions.
Follow the above and you'll be safe from people fiddling with the url to have fun with you.

Categories