I am new to working with PHP sessions.
To test code using $_SESSION I have created 2 test pages(test_session.php, get_session_test.php). I have written this code using articles on w3schools https://www.w3schools.com/PhP/php_sessions.asp
The session variables are correctly set, I have used print_r($_SESSION) on the test_session.php page which shows they have been set as in the code. However when I view the second page get_session_test.php no session variables are shown. I have even tried the print_r($_SESSION) and this only shows an empty array: Array ()
in test_session.php the print_r shows [favcolor] => green [favanimal] => cat but in get_session_test.php no data shows
Here is the code on both pages:
test_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="cat";
echo "Session variables are set";
print_r($_SESSION);
echo '<BR><BR>';
echo $_SESSION["session_id"];
?>
</body>
</html>
get_session_test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
echo '<BR>';
echo '<BR>';
echo 'Favourite color is'.$_SESSION["favcolor"].'<BR>';
echo 'Favourite color is'.$_SESSION["favanimal"].'<BR>';
?>
</body>
</html>
Related
I was studying sessions and came up with a problem. On my first page, index.php, I wrote a simple code to start a session, given as follow:
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION['name'] = 'Daniel';
echo $_SESSION['name'];
?>
<ul>
<li>index</li>
<li>contact</li>
</ul>
</body>
</html>
Next, I created a file contact.php as follow:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION['name'];
?>
This is contact page.
</body>
</html>
In contact.php the session variable $_SESSION['name']; is not displayed. But in the first file, index.php, the session variable is set and is being displayed.
What's wrong here? Why isn't the session variable in contact.php being displayed?
I want to hide a div from direct users but showing the same div to those who come from example.com
eg.
example123.com/article.php have below div
<div id="main">Title</div>
(when user click on a hyperlink on example.com
Artile
then show the above div
but when user come directly to example123.com/article.php then don't show the div.
how will I do that using php?
Hi you can use the following code like this.
<?php if (isset($_SERVER['HTTP_REFERER'])){ ?>
<div style="width:200px;height:200px;border:1px solid black">
<?php } ?>
You need to make use of $_SERVER['HTTP_REFERER']
I don't totally follow your question but this code should be enough for you to adapt to your needs:
if(strstr($_SERVER['HTTP_REFERER'],'example.com')) {
echo '<div id="main">Title</div>';
}
So, if the referrer URL contains example.com then echo your div.
If the referral URL didn't contain example.com or was empty (i.e. they arrived directly at your site) then the div won't show.
You can achieve this by passing an argument from the URL. The value of the argument will be null if they access the page directly and only have a value if they use the specific URL. Then your PHP can just check the argument and handle it accordingly.
Example as follows.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 01</h1>
Regular URL
<br />
Argument URL
</body>
</html>
Then you can handle the Arguments in your PHP page containing the div
pagewithdiv.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<div id="conditional">
<h2>Conditional Div</h2>
</div>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "block"';
echo '</script>';
} else {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "none"';
echo '</script>';
}
?>
</body>
</html>
Keep in mind though that this is only hiding the div, it still exists on the page. If you want it to be completely gone then instead of using javascript to change the visibility you can echo the code that makes up the div if the requirements are met.
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<div id="conditional">';
echo ' <h2>Conditional Div</h2>';
echo '</div>';
}
?>
</body>
</html>
Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.
Consider the following code which works fine.
<?php
require_once 'php/db_conx.php';
$Result = mysql_query("SELECT * FROM table ORDER BY lastupdated") or die (mysql_error());
while($row = mysql_fetch_array($Result))
{ ?>
<input name="1" type="submit" value=" ">
<span><?php echo $row['name'];?></span>
<?php
// then i close the PHP started at on top.
}?>
Now, the following won't print anything on print_r but just 'array()' not the session $Variable '$U' itself.
<?php
{
session_start();
$_SESSION['U'] = 'www.gmail.com';
?>
Some HTML
<?php
Print_r ($_SESSION);
}
?>
Following works fine:
<?php
session_start();
$_SESSION['U'] = 'www.gmail.com';
?>
<p>Hello</p>
<?php
Print_r ($_SESSION);
?>
and prints
Hello
Array ( [U] => www.gmail.com )
Put session_start() Beginning of the page
Check this example
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
I am new to PHP and cannot figure out why this is not working properly. What I want to happen is if a session is found execute the web page otherwise redirect to login.php. What is happening is the webpage is being executed and the user sees sql errors then the redirect happens. It's as if both the if and the else are being executed. Please help.
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>
a nicer way:
<?php
session_start();
if (!isset($_SESSION['userID'])){
header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);
?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
check your braces and do session_start before using the session
If you are using if else statements within a webpage you may find this format easier:
<?php if(true): ?>
HTML Here
<?php else: ?>
HTML Here
<?php endif; ?>
But the reason why your code isn't working is because you have got your curly braces mixed up.
Examine this example and compare it with your code:
if(true) {
// Do stuff
}
else {
// Do stuff
}
Try this code your first curtly bracket was closing instead of opening
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>