MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides

Homepage Register FAQ Members Mark Forums Read Advertise Marketplace FPSowned


Go Back   MMOwned - World of Warcraft Exploits, Hacks, Bots and Guides > Programming > Server side > PHP
Reload this Page Sessions ... The killer ... The bane of PHP
PHP Discussions about PHP

Reply
 
LinkBack Thread Tools
Sessions ... The killer ... The bane of PHP
(#1)
Old
Gizzer is Offline
Knight
Rep Power: 1
Reputation: 15
Gizzer is on a distinguished road
 
Posts: 199
Join Date: Nov 2007
Sessions ... The killer ... The bane of PHP - 03-20-2008

Lets face it the hardest thing with PHP is ... Sessions. No one can ever get these to work first time, if they do they suddenly stop working. So, I am going to simplify to idiots level.

Introduction

A Session is a SUPER GLOBAL VARIABLE. This means that you can enter some data and call it after F5ing the page or after loading 1000 differen't pages. Aslong as you don't overwrite it ofc

This is good for when you want to collect multiple pages of data, good for making someone an account with your site (It can store the users ID, this then gets passed from page to page, which can be used to call data from the database for that ID) etc ...


The Actual Code

to use/create/pass along sessions the code

session_start()

MUST BE AT THE START OF EVERY PAGE. Before any HTML. If not it will just error.

At the top of every page I just put <?php session_start() ?> at the top of every page before everything else, on line 1.



Putting data into session
Pretty easy but alot of people get stuck on it!

If you got your form ...
<html>
<form method=post action=blah.com>
<input type=text name=UserID>
<input type=submit>
</form>
</html>

And your next page is just going to be where you stick your data into the session

<?php session_start()
//Now to create the session which you want to store data in. You can call ID what ever.
$_SESSION['ID'] = $_POST['UserID'];
?>

And finally the test to see if it worked(A third page)

<?php session_start()
print("$_SESSION['ID']");
?>

Easy as ...

You can use the $_SESSION['ID'] in mysql querys and all over your page if you wish. If you are making a site which requires a login you can make sure there is something in the session, otherwise back to home page.

Deleting a session

Usually the server that your hosting on will delete sessions created after a certain amount of time. Lets say if you dont need to use that session anymore and you don't want it interfering with users reusing your site you can use the command

Session_destroy ()

This will delete any sessions. Use for a log out script or if the values of data that were stored in the session are no longer needed ...




I really, really, hope I explained this well and if you have any questions to ask go for it.

I wish I had a stupidly simple guide like this when starting sessions but all I could find was a 10 page + guide ... pfft why read all that when it could be cut down so simply?


6 rep to -1
Matt linking post in sbawx.. could've been -25 rep, read the rules

Last edited by Gizzer; 03-20-2008 at 08:01 PM. Reason: My gramah and speeling is aweful ;)
Reply With Quote

Donate to remove ads.
(#2)
Old
warsheep is Offline
Contributor
Rep Power: 4
Reputation: 184
warsheep has a spectacular aura aboutwarsheep has a spectacular aura about
 
Posts: 1,149
Join Date: Sep 2006
03-20-2008

True, sessions can really create allot of trouble when reading hard guides. Mostly because they make everything more difficult!

Good guide, I guess.


<ToXiCa> warsheep + denmark + VIP lounge + cheese = love <3
FOR A MOMENT, NOTHING HAPPENED. THEN, AFTER A SECOND OR SO, NOTHING CONTINUED TO HAPPEN.
Reply With Quote
(#3)
Old
Gizzer is Offline
Knight
Rep Power: 1
Reputation: 15
Gizzer is on a distinguished road
 
Posts: 199
Join Date: Nov 2007
03-20-2008

I tried to simplify it as heavily as posible while explaining it at the same time


6 rep to -1
Matt linking post in sbawx.. could've been -25 rep, read the rules
Reply With Quote
(#4)
Old
warsheep is Offline
Contributor
Rep Power: 4
Reputation: 184
warsheep has a spectacular aura aboutwarsheep has a spectacular aura about
 
Posts: 1,149
Join Date: Sep 2006
03-20-2008

I would rep, but I repped ya to recently :/


<ToXiCa> warsheep + denmark + VIP lounge + cheese = love <3
FOR A MOMENT, NOTHING HAPPENED. THEN, AFTER A SECOND OR SO, NOTHING CONTINUED TO HAPPEN.
Reply With Quote
(#5)
Old
tttommeke is Offline
Banned
Rep Power: 0
Reputation: 1
tttommeke is a name known to all
 
Posts: 632
Join Date: Jul 2007
03-21-2008

Sessions are easy xD cookies are weird :s
Reply With Quote
(#6)
Old
Gizzer is Offline
Knight
Rep Power: 1
Reputation: 15
Gizzer is on a distinguished road
 
Posts: 199
Join Date: Nov 2007
03-21-2008

Guess I could do cookies next when I have the time


6 rep to -1
Matt linking post in sbawx.. could've been -25 rep, read the rules
Reply With Quote
(#7)
Old
Sirupsen's Avatar
Sirupsen is Offline
Knight-Lieutenant
Rep Power: 1
Reputation: 16
Sirupsen is on a distinguished road
 
Posts: 228
Join Date: Oct 2007
Location: Denmark
03-21-2008

I miss information about the lasting of a session, and more examples of what you can use it for. (For "Who's online list" etc etc. So the new coders know why they are good.) And information about how long how much time in minutes it takes before it get's automaticly deleted? And that it get's deleted when you close your browser.


- Sirupsen
Reply With Quote
(#8)
Old
Gizzer is Offline
Knight
Rep Power: 1
Reputation: 15
Gizzer is on a distinguished road
 
Posts: 199
Join Date: Nov 2007
03-22-2008

Session lasts untill its destroyed, times out by the server or edited.

How long it times out is set by the server. If your hosting on someone elses server you will need to contact them about it, if its your server you will just have to have alook around. It is not destroyed when you close your browser, its destroyed when you reopen your browser and your browser sends out a ping saying "Are you there", something like that but I am not too sure on that one.

When I made a whose online list ... I am yet to learn how to access hosts sessions. I have only been using paid hosting servers. I just made it so everytime someone makes a click it records the time you made the click. If your last click on the site was less than 15 mins ago ... show as active


My guide was generally a begginers guide that wasn't going to be too complex.


6 rep to -1
Matt linking post in sbawx.. could've been -25 rep, read the rules
Reply With Quote
(#9)
Old
insanesk8123's Avatar
insanesk8123 is Offline
Contributor
Rep Power: 3
Reputation: 249
insanesk8123 has a spectacular aura aboutinsanesk8123 has a spectacular aura aboutinsanesk8123 has a spectacular aura about
 
Posts: 579
Join Date: Feb 2007
Location: main(String[] args)
03-22-2008

I really needed a guide like this


By Jerbab
Reply With Quote
(#10)
Old
Sirupsen's Avatar
Sirupsen is Offline
Knight-Lieutenant
Rep Power: 1
Reputation: 16
Sirupsen is on a distinguished road
 
Posts: 228
Join Date: Oct 2007
Location: Denmark
03-22-2008

Quote:
Originally Posted by Gizzer View Post
Session lasts untill its destroyed, times out by the server or edited.

How long it times out is set by the server. If your hosting on someone elses server you will need to contact them about it, if its your server you will just have to have alook around. It is not destroyed when you close your browser, its destroyed when you reopen your browser and your browser sends out a ping saying "Are you there", something like that but I am not too sure on that one.

When I made a whose online list ... I am yet to learn how to access hosts sessions. I have only been using paid hosting servers. I just made it so everytime someone makes a click it records the time you made the click. If your last click on the site was less than 15 mins ago ... show as active


My guide was generally a begginers guide that wasn't going to be too complex.
Well, okay. I knew about that stuff. But just I'm quite sure some people might be intrested in it?


- Sirupsen
Reply With Quote
(#11)
Old
Runez is Offline
Master Sergeant
Rep Power: 1
Reputation: 6
Runez is an unknown quantity at this point
 
Posts: 87
Join Date: Mar 2008
03-28-2008

Definetly worth mentioning session expiration and the likes.

If theres anyone who want a "cookies guide" and you dont have time to do it anytime soon post it here and ill write one.
Reply With Quote
Reply

Donate to remove ads.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On




Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
vBulletin Skin developed by: vBStyles.com


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342