/    Sign up×
Community /Pin to ProfileBookmark

php session variables

Hello.

I coding a site. I intend the user to see a sign “Welcome” displayed on the top of index.php, when they enter to see the first page (supposing that that is the page they arrive at the site). Then the user can go to see other pages in the site, eg products.php or contact.php and later when they return to index.php the sign “Welcome” will no longer be there (they have been already welcomed).

I have approached the problem with php session variables. Here is my code of index.php:

[code]if($_SESSION[“mynumber”]!=1){include ‘welcome.txt’;}
else{$_SESSION[“mynumber”] = 1;include ‘welcomealternative.txt’;}[/code]

I find this problem difficult. If I set a session variable to 1 when a user enters index.php, and then increase it on every other page by one, when they return to index.php, it will be given value of one and the user will be welcomed. Which is not good.

The problem can get more difficult if we consider cases, when the user arrives to the site to a page different than the index.php…

What is the best solution here?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 21.2021 — Without understanding _why_ you are incrementing this number elsewhere, seems like you could just increment it at this point, too, instead of setting it to 1 -- or just leave it alone?
[code=php]
if(empty($_SESSION["mynumber"])) {
include 'welcome.txt';
} else {
$_SESSION["mynumber"] += 1;
include 'welcomealternative.txt';
}
[/code]
Copy linkTweet thisAlerts:
@codewitchauthorJul 21.2021 — Yes! Thank you NogDog. The increasing by one everywhere can be used to track number of pages a user visits in one session.

I didn't know that a non-declared variable has a value of zero.

I didn't know about the empty() function in php. Thank you.
Copy linkTweet thisAlerts:
@NogDogJul 22.2021 — > @codewitch#1634512 I didn't know that a non-declared variable has a value of zero.

Well...it sort of does, but it sort of doesn't. If you try to use it like a number, PHP will cast it to a zero and throw a notice or warning, but it's not fatal (normally). In my example, if $_SESSION['mynumber'] is not set at all, then it will still not be set, since the code in the else block will only execute if (a) it is set and (b) it has a non-zero, non-null value. So, if you want hitting that page for the first time count as a hit, you could change it to:
[code=php]
if(empty($_SESSION["mynumber"])) {
$_SESSION["mynumber"] = 1;
include 'welcome.txt';
} else {
$_SESSION["mynumber"] += 1;
include 'welcomealternative.txt';
}
[/code]
×

Success!

Help @codewitch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...