PHP - Loosing Session Id Again :(
While the user is logged in, i want it to keep my session id when I move from link to link. But its loosing it
When clicking on the link it first goes to this part of code <? session_name ('YourVisitID'); ini_set('session.use_cookies', 0); // Don't use cookies. session_start(); $session = session_name('YourVisitID'); include ('./includes/header.html'); ?> <html> <meta http-equiv="refresh" content="0;url=showpage.php?page=1&YourVisitID=".SID.> </html> Then that redirects to this code <? session_name ('YourVisitID'); ini_set('session.use_cookies', 0); // Don't use cookies. $session = session_name('YourVisitID'); session_start(); include ('./includes/header.html'); ?> <html> <center> <h2>Becky's Photo Gallery</h2> Please, click each thumbnail image to see more details. Thanks. <p> <?php #### Resizing Function ######### function imageResize($width, $height, $target) { if ($width > $height) { $percentage = ($target / $width); } else { $percentage = ($target / $height); } $width = round($width * $percentage); $height = round($height * $percentage); return "width=\"$width\" height=\"$height\""; } ################################# echo ("<table border=1 cellpadding=5 cellspacing=5><tr>"); $page=$_GET['page']; $mydir = dir('./data'); # read every file in the directory of "data" while(($file = $mydir->read()) !== false) { if (stristr($file, "JPG")){ # only JPG files $total++; if ($total>28*($page-1) && $total<=28*($page)){ $count++; $mysize = getimagesize("./data/$file"); echo ("<td width=100 align=center valign=middle><a href=./view/show.php?id=$total>"); echo ("<img src=./data/$file "); echo (imageResize($mysize[0], $mysize[1], 100)); # thumbnail size 100 pixel echo (" border=0></a></td>"); if ($count>6){ echo ("</tr><tr>"); $count=0; } } #total<15 } } # fill out the blank cells with * if ($count>0){ for ($i=$count; $i<7; $i++){ echo ("<td align=center width=100>*</td>"); } echo ("</tr>"); } echo ("</table><p>"); # prepare the navigation menu for more images/pages $n=((int)($total/29))+1; for ($i=1; $i<$n+1; $i++){ if ($i==$page){ echo ("<b>$i</b> "); } else { echo ("<a href=showpage.php?page=".$i."".SID.">".$i."</a> "); # page navigation; 28 images per page } } ?> </center> </html> which on there should go to this code <?php session_name ('YourVisitID'); ini_set('session.use_cookies', 0); session_start(); // Start the session. ?> <html> <style type="text/css"> body { font-family: Georgia; background-color: #666666; } a { text-decoration: none; color: #FFFFFF; } a:hover { color: #888888; } </style> <center> <?php # this function is for resizing a given image, currently set with 600 pixel. function imageResize($width, $height, $target) { if ($width > $height) {$percentage = ($target / $width); } else { $percentage = ($target / $height); } $width = round($width * $percentage); $height = round($height * $percentage); return "width=\"$width\" height=\"$height\""; } # gather necessary information from the previous page with GET method $id=$_GET['id']; $interval=$_GET['interval']; $last=$_GET['last']; # if the slide show is on, show the next one, if nothing left, back to the first image if ($interval){ echo ("<font size=2>Slid Show ON with $interval Seconds of Interval!</font><p>"); $forward=$id+1; if ($forward>$_GET['last']){$forward=1;} echo ('<META HTTP-EQUIV=Refresh CONTENT="'.$interval.'; URL=show.php?id='.$forward.'&interval='.$interval.'&last='.$last.'">'); } # read every file from "data" directory (folder), and get only JPG files. case insensitive for "JPG" $mydir = dir('../data'); while(($file = $mydir->read()) !== false) { if (stristr($file, "JPG")){ $count++; if ($id==$count){ $filename="../data/".$file; $current=$id; } } } # set the current image, and the previous and the next one by number (+/-) $last=$count; $previous=$current-1; $next=$current+1; if ($current==1){$previous=$last;} # avoid the minus number if ($current==$last){$next=1;} # avoid the out of range number # get the image size into an array $mysize = getimagesize("$filename"); echo ("<a href=show.php?id=".$previous.">Previous Image</a> | "); echo ("<a href=http://vanselow.soisuwm.com/final/index.php>HOME</a> | "); echo ("<a href=show.php?id=".$next."".SID.">Next Image</a>"); echo ("<p>"); echo ("<a href=$filename target=_blank ".SID."><img src=$filename "); if ($mysize[0]>600 && $mysize[1]>600){ # width AND height are greater than 600 pixel echo (imageResize($mysize[0], $mysize[1], 600)); # resize upto 600 pixel } echo ("border=0></a>"); echo ("<p>"); ?> <form action=show.php> Change Slide Show Interval: <SELECT NAME="interval"> <OPTION value=5>5 seconds <OPTION value=10>10 seconds <OPTION value=15>15 seconds </SELECT> <input type=hidden name=id value=<? echo $id; ?>> <input type=hidden name=last value=<? echo $last; ?>> <input type=submit value="GO!"> </form> <p> <font size=2 color=aqua>For the full screen display, press F11 key.</font> </center> </body> </html> Yet its getting lost somewhere. what am i doing wrong... Help Thanks ! Similar TutorialsHello I have this code that searches multiple tables. running this query in the phpmyadmin page works fine and gives correct results. I echo-ed the $kw and noticed that it is lost (empty) when I click on Next (when moving to the second page or so..) How do I save the $kw for all search results? Thank you so much $kw = trim($_POST['keyword']); $kw = mysql_real_escape_string($kw); //Nomber of rows to display per page $page_rows=20; //This checks to see if there is a page number. If not, it will set it to page 1 if (!(isset($pagenum))) { $pagenum = 1; } echo "$kw of $pagenum"; //Count we count the number of results $data = mysql_query(" SELECT `id`,`title`, `body`, 'condoms_en' as REF FROM `condoms_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'discr_en' as REF FROM `discr_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'diseases_en' as REF FROM `diseases_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'express_en' as REF FROM `express_en` where `title` like '%$kw%' OR `body` like '%$kw%' ") or die(mysql_error()); $rows = mysql_num_rows($data); if ($rows ==1 || $rows > 1) { //echo "Number of rows $rows <br/>"; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //echo "<h4>".$pagenum."-20 of ".$total."results</h4> <br />"; // This shows the user what page they are on, and the total number of pages echo " ===> Page $pagenum of $last <br/><br/>"; //This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query(" SELECT `id`,`title`, `body`, 'condoms_en' as REF FROM `condoms_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'discr_en' as REF FROM `discr_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'diseases_en' as REF FROM `diseases_en` where `title` like '%$kw%' OR `body` like '%$kw%' UNION SELECT `id`,`title`, `body`, 'express_en' as REF FROM `express_en` where `title` like '%$kw%' OR `body` like '%$kw%' $max") or die(mysql_error()); //This is where you display your query results while($info = mysql_fetch_array( $data_p )) { print $info['title']; //print $info['REF']; echo "<br>"; } echo "<p><br/>"; // First we check if we are on page one. If we are then we don't need a link to the previous page //or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($pagenum == 1) { } else { echo " <a style=\"color: #FF0000\" href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a style=\"color: #FF0000\" href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } //just a spacer echo " ---- "; //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a style=\"color: #FF0000\" href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a style=\"color: #FF0000\" href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } } //results was found else { echo "No results FOUND"; } I am trying to create an index page which contains registration and login field the problem that i get is on successful login a warning is displayed session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Eventz.com\index.php:116) in C:\xampp\htdocs\Eventz.com\index.php on line 235 This is the login part of my index.php this tag is inside an html table below the login form I also have a registration form and its php code above the login form Code: [Select] <?php if (isset($_REQUEST['pass'])) { $id=$_POST['id']; $pass=$_POST['pass']; $conn =mysql_connect("localhost","root",""); if (!$conn) { die('Could not connect: ' . mysql_error()); } /* checking connection....success! */ $e=mysql_select_db('test', $conn); if(!$e) { die(''.mysql_error()); } else { echo 'database selected successfully'; } if (isset($_REQUEST['id']) || (isset($_REQUEST['pass']))) { if($_REQUEST['id'] == "" || $_REQUEST['pass']=="") { echo "login fields cannot be empty"; } else { $sql=mysql_query("Select email,password from login where email='$id' AND password='$pass'"); $count=mysql_num_rows($sql); if($count==1) /* $count checks if username and password are in same row */ { session_start(); $_SESSION['id']=$id; echo "</br>Login Successful</br>"; } else { echo "</br>invalid</br>"; echo "please try to login again</br>"; } } } } ?> Any help or suggestion would be appreciated I am having trouble resolving an error. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/s519970/public_html/header.php:27) in /home/s519970/public_html/admin/login.php on line 2 What I can gather is I can't use "header (Location: 'admin.php')" after i've used session_start(). I have tried to replace the header (Location: 'admin.php') with this: echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; I've been trying to read up on solutions but haven't been able to get it sorted. If anyone can offer some advice that would be greatly appreciated as im new to php. Code: [Select] <?php session_start(); if(isset($_SESSION['user'])) echo "<script>document.location.href='admin.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; ?> <div id="loginform"> <form action="dologin.php" method="post"> <table> <tr> <td><span>Username:</span></td> <td><input type="text" name="username" /></td> </tr> <tr> <td><span>Password:</span></td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td> </tr> </table> </form> </div> I have tried using require_once('yourpage.php'); before my <head></head> tags in the header document where I've specified the html information but this doesn't seem to work. I've been advised to use ob_start("ob_gzhandler"); but I am not sure how to implement this. Any advice is greatly appreciated! in this page http://maximaart.com/newscp/ i have this problem Code: [Select] Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/maximasy/public_html/newscp/index.php:1) in /home/maximasy/public_html/newscp/index.php on line 2 my source code is <?php session_start(); include_once("config.php"); include_once("functions.php"); $errorMessage = ''; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === "$user" && $_POST['txtPassword'] === "$pass") { // the user id and password match, $_SESSION['basic_is_logged_in'] = true; require("main.php"); exit;?> I'm making a simple login system with MySQL and PHP (very simple, I'm just starting with PHP). The MySQL portion is done, but I need to ensure only people who are logged in can see certain content. To check if people are logged in, my website checks that they have the $_SESSION['user'] variable set. If it is set, then it lets them continue through the website, if not, it tells them to login. Is that enough security, or can people simply inject a session cookie into their browser to spoof that they are logged in? My idea was to generate a session key cookie when they login (just a random string of letters and numbers) and store that in the database, then on every page, check to make sure their session key is the same thing that's in the database. Is this necessary? It seems expensive. hi everyone. i'm wondering what the best way is to create a session variable and pass it to an iframe. i need to do something along these lines, but it doesn't seem to pass the ID. Any hints on how i should accomplish this? Code: [Select] session_start(); $_SESSION['ID']=$_GET['ID']; // id from previous page $ID=session_id(); <iframe src="iframepage.php?ID=<?php echo $ID; ?>" style="width:680px; height:200px;" noresize="noresize" frameborder="0" border="0" scrolling="Yes" allowtransparency="true" /> </iframe> Just curious how other people feel about this. I am working on an application where a lot of info is pulled from MySQL and needed on multiple pages.
Would it make more sense to...
1. Pull all data ONCE and store it in SESSION variables to use on other pages
2. Pull the data from the database on each new page that needs it
I assume the preferred method is #1, but maybe there is some downside to using SESSION variables "too much"?
Side question that's kind of related: As far as URLs, is it preferable to have data stored in them (i.e. domain.com/somepage.php?somedata=something&otherdata=thisdata) or use SESSION variables to store that data so the URLs can stay general/clean (i.e. domain.com/somepage.php)?
Both are probably loaded questions but any possible insight would be appreciated.
Thanks!
Greg
Edited by galvin, 04 November 2014 - 10:30 AM. Evening! I've been iffing and ahhing over this and well im not too sure, hence the post. Code: [Select] // Redirects if there is no session id selected and echos the error on the previous page if(!isset($_GET['get']) || ($_GET['getget'])){ header("Location: #.php?error"); } So it should simply check if get is set if it isnt then see if getget is set? If not redirect and show the error. Now ive tried it and even when get/getget is set it still redirects, probably something silly. Care to share anyone? Harry. Hello I keep having this issue with sessions. I have an login feature on my website that uses sessions. When i do like this: 1. Login 2. close browser 3. open browser again 4. try to log in It fails, actually nothing happens, so I manually need to go to logout.php to reset and then I can log in again. Why is this happening and how do i solve it? Pls tell me what is wrong? <?PHP include("dba.php"); function hvataj ($trazi, $id) { $upit = mysql_query ('select * from administrator where member_id = '.$id.''); return ( $row = mysql_fetch_assoc ($upit) ) ? $row[$trazi] : mysql_error (); } ?> I have parse error here on this line when I want to echo it: <?PHP echo "<img src='images/korisnik_slike/".hvataj('slika',$_SESSION['member_id']."' />"; ?> This is weird. I thought session_destroy erased your session id: echo "session is ".session_id(); session_destroy(); echo "<br>session now is ".session_id(); When I run it i get: Code: [Select] session is mkqb0m49ktbtt1g34r6e67g012 session now is But when I reload the page I get the same thing! Code: [Select] session is mkqb0m49ktbtt1g34r6e67g012 session now is If session_id was destroyed (it echoed blank to prove this) why does it get repopultaed with the same session id when I reload the browser? I am producing a php script which will include some AJAX in the resultant web page, which will send some data to another php script for processing. If I create a php session in my original script, will the same session be accessed by the second script being run by the AJAX or will it create a new session? Code: [Select] <?php session_start(); if(!session_is_registered(myusername)){ header("location:./default.php"); } ?> My session works fine but I am just wondering how I can echo the current sessions username? Thanks If i use session in my php code then for how much time the session will be valid... And how can i change the length of the session... Hi I am looking for help with a working script to input multiple lines of data , but i am looking for each line of data to have the $_SESSION[userid] entered into each line, ie into the table $_SESSION[userid], name, surname, age Many Thanks <? include "include/session.php"; include "include/z_db.php"; // check the login details of the user and stop execution if not logged in require "check.php"; // If member has logged in then below script will be execuated. // let us collect all data of the member $row=mysql_fetch_object(mysql_query("select * from plus_signup where userid='$_SESSION[userid]'")); if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name= mysql_real_escape_string($person['name']); $surname= mysql_real_escape_string($person['surname']); $age= mysql_real_escape_string($person['age']); $values[] = "('$name', '$surname', '$age')"; } $query = "INSERT INTO plus_signup (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); } ?> <form method="POST" action='multi.php' name=""> <table> <tr> <td>Name<input type="text" name="person[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person[1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> <tr> <td>name<input type="text" name="person[2][name]"></td> <td>surname<input type="text" name="person[2][surname]"></td> <td>age<input type="text" name="person[2][age]"></td> </tr> </table> <td><p> <input type="submit" name="submit" value="add"> </p> <p> </p>| <a href=update-profile.php>back</a>| </form> <? require "bottom.php"; ?> <center><br>| <a href=update-profile.php>back</a>|<br></center> </body> </html> I am building an app (PHP and MySQL)and I had been using a lot of GET calls to get info from URLs, but the more I thought about it, the more I didn't like the possibility of people being able to mess with the URLs. So I am in the process of changing everything to use SESSION variables to store data across pages, rather than GET. The way I see it, SESSION variables are completely behind the scenes so they seem to be the better option. Am I right, or is GET better than SESSION for some reason? I am having trouble calling the session var "email" from the landing page. Here is the code that I am using. I am not even sure the session "email" is starting or registering. Code: [Select] $referer = $_SERVER['HTTP_REFERER']; $email = $_POST['email']; $sql="SELECT * FROM users WHERE email='$email' "; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $email, table row count must be 1 row if($count==1){ // Session Register email session_start(); $_SESSION['email'] = $email; header("location:".$referer2." "); exit(); } Is there anyway to work out the age of a session, i.e the time that has elapsed since it was created? Cheers Why is my SESSION not getting set in the code below?! Code: [Select] <?php // Initialize a session. session_start(); // Initialize Logged-In Status. $_SESSION['loggedIn'] = FALSE; // Display Logged-In Status. echo '<p>$_SESSION[\'loggedIn\'] = ' . $_SESSION['loggedIn'] . '</p>'; exit(); When I run this I get... $_SESSION['loggedIn'] = Debbie |