Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP5.4 Global or Session Variables?

Status
Not open for further replies.

sunbase

Technical User
Jun 18, 2009
15
0
0
GB
I have a simple problem that I cannot seem to sort in 5.4. I have a website login which validates user login with the usual ID and password requirements. What I am truing to do in 5.4 which I successfully achieved in PHP4 is to create a static or session variable to hold a value which signifies authorised so that one sign in will give me a way of authorising access on each page.

I understand session variables are not available in PHP 5.4 so can anybody tell me how I can create a variable on user sign in that will be available from that point on? in PHP4 I created a session variable $authy and on each page I could check the value and allow access or not depending on result

if(@$authy !="1")
{
header("Location: notlog.php");
exit();
}

Please keep answers simple please as the move from PHP4 to 5.4 is proving traumatic to say the least.

 
Hi

sunbase said:
I understand session variables are not available in PHP 5.4
Not exactly. Just using the [tt]session_*()[/tt] functions.

PHP Documentation said:
Warning This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
( PHP Manual | Function Reference | Session Extensions | Sessions | Session Functions | [tt]session_register()[/tt] )

Use the [tt]$_SESSION[/tt] array instead :
PHP:
[b][COLOR=#000000]if[/color][/b] [COLOR=#008080]([/color][highlight]SESSION[/highlight][COLOR=#008080][[/color][i][COLOR=#009900]'authy'[/color][/i][COLOR=#008080]][/color] [COLOR=#008080]!=[/color] [i][COLOR=#009900]"1"[/color][/i][COLOR=#008080])[/color]
[COLOR=#008080]{[/color]
    [COLOR=#FF6600]header[/color][COLOR=#008080]([/color][i][COLOR=#009900]"Location: notlog.php"[/color][/i][COLOR=#008080]);[/color]
    [b][COLOR=#000000]exit[/color][/b][COLOR=#008080]();[/color]
[COLOR=#008080]}[/color]

Feherke.
feherke.ga
 
session variables are ALWAYS temporary no matter what version of PHP you are using.

cookies are for persistent/static storage of variables


I understand session variables are not available in PHP 5.4
That is incorrect, you have misread or misunderstood something there.

It is simply that session_register() has been removed in 5.4


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi

Grr ! Something messing things up in the highlighter. In the code block the variable should be used with sigil and leading underscore : [tt][highlight]$_[/highlight]SESSION['authy'][/tt].


Feherke.
feherke.ga
 
So I can still read session variable using $_SESSION['authy']. but how do I assign a value to it in the first place? All references I can find in manual simply say <PHP5.4

Apologies if asking ultra-obvious stuff but I have tried and, as they say "if you dont ask". Your time and patience greatly appreciated
 
Hi

sunbase said:
how do I assign a value to it in the first place?
Well, just like that : assign to it.
Code:
[COLOR=#008080]<?php[/color]
[COLOR=#FF6600]session_start[/color][COLOR=#008080]();[/color]
$_SESSION[COLOR=#008080][[/color][i][COLOR=#009900]'authy'[/color][/i][COLOR=#008080]][/color] [COLOR=#008080]=[/color] $_POST[COLOR=#008080][[/color][i][COLOR=#009900]'username'[/color][/i][COLOR=#008080]][/color] [COLOR=#008080]==[/color] [i][COLOR=#009900]'foo'[/color][/i] [COLOR=#008080]&&[/color] $_POST[COLOR=#008080][[/color][i][COLOR=#009900]'password'[/color][/i][COLOR=#008080]][/color] [COLOR=#008080]==[/color] [i][COLOR=#009900]'bar'[/color][/i][COLOR=#008080];[/color]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top