PHP remember url parameter value using $_SESSION - php

I made a script that shows the value of "school_id" in url parameter.
http://mywebsite.com/mygrade?school_id=00000
I use $_GET['school_id'] to display the ID number.
<?php echo $_GET['school_id']; ?>
But I what I want is if the parameter "school_id" is empty, I want to display the previous data entered.
Example, the user already browse http://mywebsite.com/mygrade?school_id=00000 then he browse http://mywebsite.com/mygrade?school_id= which id has no value. It will still display 00000 which is the previous ID he used.
I used this code below but doesn't work.. :(
<?php
session_start();
$_SESSION['schoo_id'] = $_GET['school_id'];
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
}
?>
Anyone who get my point and could help me?

I'm going to break this down line by line, please let me know in the comments if I need to explain anything further:
Self explanatory:
<?php
session_start();
There is a typo here:
$_SESSION['schoo_id'] = $_GET['school_id'];
But! Fixing it won't resolve your problem. What happens if $_GET['school_id'] is not defined/blank? Guess what, $_SESSION['school_id'] is now blank. Obviously you don't want this behavior, so you'll want to only set $_SESSION['school_id'] if $_GET['school_id'] is defined
accessing $_GET['school_id'] will throw an E_NOTICE error if it isn't defined, so you'll want to instead check its existence, rather than checking to see if it is null.
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
Oh, that typo was intended. Why misspell school though? No need! :)
echo $_SESSION['schoo_id'];
What is this doing? Nothing! No echo, nothing. Just accessing a variable and doing nothing with it.
}
else{
$_GET['school_id'];
}
?>
Here's what your code should look like, or at least I believe is what you intend:
<?php
session_start();
if (isset($_GET['school_id']) && $_GET['school_id'] !== ""){
$_SESSION['school_id'] = $_GET['school_id'];
}
// $_SESSION['school_id'] will be guaranteed to be what $_GET['school_id'] is (if set)
// or whatever it was last time it was defined
// always echo it.
echo $_SESSION['school_id'];
?>

<?php
session_start();
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
$_SESSION['schoo_id'] = $_GET['school_id']; //here set the session
}
?>

I agree with Salman A, the simplest way:
<?php
session_start();
if (is_int($_GET['school_id'])) $_SESSION['school_id'] = $_GET['school_id'];
// further use $_SESSION['school_id'] for your needs.
?>

what you need to do here is save the GET value in SESSION only if it is set for later use so this should work
<?php
session_start();
if (!isset($_GET['school_id']) || $_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_SESSION['schoo_id'] = $_GET['school_id'];
echo $_GET['school_id'];
}
?>

You almost have it.
<?php
session_start();
if (isset($_GET['school_id']) && trim($_GET['school_id']) !== '') {
// its a fair assumption to make that 'school_id' is intended to be an integer,
// however I will not make that assumption on the OP's behalf.
$_SESSION['school_id'] = $_GET['school_id'];
}
if (isset($_SESSION['school_id']) {
echo $_SESSION['school_id'];
}
else {
echo 'have not entered a school id yet';
}
?>

Related

how to pass session value other page if condition true in php

i have a $_sesstion['usermail']. i want to pass this value to next page.if condition match ($answer= $_SESSTION['usermail']);
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
header("Location:resetpass.php");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
i want to pass $_sesstion['usermail'] value on resetpass.php page.
I think your logic is wrong here. What exactly are you checking in the if statement. A session variable means you can use it on every page that has session_start(); on top.
Sessions by default pass to other pages.
Make sure you have start_session(); on top of the page you want to access the session variable.
So if $_SESSION['usermail'] is working on your current page, it'll work on your next as well with same data.
Get an idea from this exmple
First Page
<?php
session_start();
$_SESSION['name'] = "Adam";
?>
Second page
<?php
session_start();
echo $_SESSION['name'];
?>
You can use GET methods for sharing your session value to next page...
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$value_to_share=$_SESSION['usermail']; // You can share using GET
header("Location:resetpass.php?value=$value_to_share");
// receive this value at resetpass.php by $_GET['value']
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}

Why my cookies are not saving?

I have a problem with cookies. In my login script i have the following line of code:
if($_GET['keep'] == "true"){
setcookie('id',$id,time()+3153600);
}
The problem I'm facing is that the cookies are not saving at all ( not even if i don't quit the browser). I'm quite a beginer in this respect and I think I'm not doing it right.
EDIT:
If i print_r all the Cookies it only gives me PHPSESSID after the cookie is set. I printed on index.php and i set the cookie on login.php
SOLUTION: Cookies are saved by default with the path of the file they were created in. To change the path there is another atribute. So by setcookie('id',$id,time()+3153600,'/'); you make the cookie available for the entire domain.
There is no issue in your code
if($_GET['keep'] = "true"){
setcookie('id',$id,time()+3153600);
}
This will may cause to
No data passing to $_GET['keep']
Or if data passing $_GET['keep'] value in not Matched ("true").
Both Works then $id is empty in setcookie method
Improve your code
if(isset($_GET['keep']){
if($_GET['keep'] == "true"){
if(isset($id))
{
#all perpect
$cokkie_id = 'id';
setcookie('id',$id,time()+3153600);
echo "I'm Set. And My value is ".$cokkie_id;
}
else
{
echo "Opzz My ID is also empty";
}
}
else
{
echo 'Get method is Set. But Value is not "true". Actual value is '. $_GET['keep'];
}
}
else
{
echo 'I cant reach Get method Buddy';
}
I think you miss "=" sign
if ($_GET['keep'] == "true") {
if (!isset($_COOKIE['id'])) {
setcookie('id',$id,time()+3153600);
}
}
use isset or ==
if (isset($_GET['keep']) && $_GET['keep'] == "true") {
setcookie('id', $id,time()+3153600);
}else{
echo 'keep is empty';
}

Need help making an if statement in php that creates a html href link when passed correctly

<?php if ($_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
i want the link to only be created if the IF statement is successful and to not create it if it is not. At the moment no matter what i have tried to change the link will always be created. thanks.
Have you tried
<?php
if ($_SESSION["isLoggedOn"] == True)
{
echo 'LOGOUT';
}
?>
Your this visible code seems to be corrected. May be you would have some error in storing the sessions. Try:
echo $_SESSION['isLoggedOn'];
OR try doing this:
<?php
if ($_SESSION['isLoggedOn']) {
echo "Logout";
}?>
<?php if (isset($_SESSION["isLoggedOn"]) && $_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
Your $_SESSION["isLoggedOn"] is always TRUE.
Also dont use == TRUE for True/False variables.
Also logging out at terms and conditions page is not very good i guess ;-)
Elaborate your code:
<?php
session_start();
if ( !isset($_SESSION) || !isset($_SESSION["isLoggedOn"]) {
echo "an error occured instantiating the session.";
exit 1;
}
if ( $_SESSION["isLoggedOn"] === true ) {
echo 'LOGOUT';
}
?>
You need to start a session as very first action in your PHP.
Check if you actually have a vale in $_SESSION
Then, check its state and output what you want.

PHP If and else statement -

I am pulling images and information from a mySQL database and displaying with a few PHP functions. At times all of the information isn't there and I need it to basically display:none; but I can't seem to get it - What am I missing? Here is my display function:
<?php if ($recipe->hassliderimage5 == true) {
$recipe->show_image_carousel5();
} else {
}
?>
And here is the PHP function calling it from the database -
if (trim(mysql_result($this->result,0,"imageCarousel5") != '')) {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}
Here is what I got to work for what I wanted - not sure if it is the best solution or not? I'm still kind of new to PHP.
<?php if ($recipe->hassliderimage5 == true) { ?>
<div id="sliderimageFive" class="item">
<?php
$recipe->show_image_carousel5();
?>
</div>
<?php } ?>
You seem to make the mistake to create your view before collecting the data that has to be displayed. A more logical application structure would be to fetch the data from the database, validate it and then create your view according to the amount and type of data you have.
Also, take a look at this: Why shouldn't I use mysql_* functions in PHP?
I can't comment cause i have not enough reputation, so i try it with an answer.
why you are using the trim on mysql_result($this->result,0,"imageCarousel5") != '' this code will give you either true or false no string you have to trim
Maybe you want to trim the return of mysql_result and then check if its empty
if (trim(mysql_result($this->result,0,"imageCarousel5")) != '') {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}
Try this test:
<?php if (filter_var(recipe->hassliderimage5, FILTER_VALIDATE_BOOLEAN) == true) {
$recipe->show_image_carousel5();
} else {
// do other
}
?>
if return true if the value of 'recipe->hassliderimage5' is "1", "true", "on" and "yes"
Enjoy your code!

Check session and cookie not working in PHP

I have this code that makes sure your are logged in, and then making sure you are on the right page by checking a cookie set at login. This code works on a page in a directory underneath the login in script, however in a page in a directory below that it always takes you to accessdenied. Any ideas?
<?php
session_start();
if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
header("location: http://mywebsite.com/member/accessdenied.html");
exit();
}
$_COOKIE["verify"] = $verify;
if( $verify != file_get_contents("name.txt")) {
header("location: http://mywebsite.com/member/accessdenied.html");
} else { }
?>
And it seems like just the bottom part, the part that checks the cookie, isn't working. Again, any ideas?
I think you have your cookie assignment backwards:
$_COOKIE["verify"] = $verify;
Should be
$verify = $_COOKIE["verify"];
And that should be:
$verify = isset($_COOKIE["verify"])?$_COOKIE["verify"]:false;
As if the cookie was not previously set, well it would give a notice error.
<?php
$verify = $_COOKIE["verify"];
if( $verify == file_get_contents("name.txt")) {
echo $verify . 'is equal to the content of name.txt'
} else {
echo $verify . 'is NOT equal to the content of name.txt'
}
?>
Try debugging the code with this. See if the content of your variable is what you want. But I find it unusual that a variable would be a file.
are you sure you always get the content from file_get_contents? I could imagine it's found in one directory but not in the other!
antoher idea: cookies can be set to be relevant for a particular directory only. I just realize, what we're missing here, is the part where you set the cookie in the first place.

Categories