Need help in display members name when logged in using PHP? - php

for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.

Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)

Make sure you're also calling session_start() before trying to access the variables.

Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>

This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?&gtl
Is it?

Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Related

Show url only if value is present

Goal:
Instead of showing simple YES or NO.
If value is found in record, show hyperlink with that value or else show text "No"
How to modify below code for this purpose:
<?php echo $row_RecordsetContacts['propertyFile'] ? '<strong>Yes</strong></br>' : 'No</br>'; ?>
View
</td>
<?php
$file = $row_RecordsetContacts['propertyFile']; # for readability only
if ($file)
{
?>View<?php
}
else
{
?>No<?php
}
I also suggest to avoid mixing echo and HTML markup. In 99% cases it makes the code better for understanding.
Try the following code:
<?php
$prop = $row_RecordsetContacts['propertyFile'];
if(empty($prop)) {
echo "No";
} else {
echo "<a href='propfiles/$prop'>View</a>";
}
?>

PHP - Add and stack a value with a session

Let's say I have a $variable, which contains the word "hey" and a link with a GET parameter.
HTML
Link
PHP
$var = "hey";
if ($_GET['add'] == 'true') {
$var .= "2";
echo $var;
}
When I click on the link, it will add "2" to the variable value "hey", so the
output is: hey2.
Is there a way of keep adding (stacking) the numbers on click with pure PHP?
If i click again, I'd like to have hey22, hey222 (...)
EDIT: It looks like it isn't going to work with variables, so the best answer goes to #Bunker Boy because he solved it with sessions.
#Syno try this:
// mywebsite.html
Link
//mywebsite.php
<?php
session_start();
if(isset($_SESSION["var"])){
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}
else{
$_SESSION["var"] = "hey";
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}

How to set a PHP variable via URL?

I'm trying to cause a variable $btn1Pressed to be set via a URL load. For example, loading http://mywebsite.com/myphp.php?btn1Pressed=1 would set the variable to 1. The below test code doesn't seem to be doing anything:
<?php
if ($btn1Pressed == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>
Before the vampires arrive...
<?php
if (isset($_GET['btn1Pressed']) && $_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
?>
Anything in the query string will be in PHP's $_GET array. To see the entire array you can print_r($_GET); in your PHP code. In the example I am also testing to make sure the variable has been set, for safeties sake. You should never accept user input without sanitizing, which I have not done here.
You can also set a variable with the array item:
$btn1Pressed = $_GET['btn1Pressed'];
You can try use GET method:
if ($_GET['btn1Pressed'] == 1) {
echo 'Button One Pressed';
}
else{
echo 'Button Two Pressed';
}
<?php
$btn_pressed = filter_input(INPUT_GET, 'btn1Pressed', FILTER_SANITIZE_NUMBER_INT);
if ($btn_pressed == 1) {
echo 'Button One Pressed';
} else {
echo 'anything';
}
?>

GET request variable error in PHP

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

Passing a PHP Value from a link

I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>

Categories